size optimising; help needed
ok,
This loop iterates over an int array, and places in it a randomly generated X, Y and color value.
I want to size optimise the code, I don't care if the positions are no-longer random, I simply want them to *look* randomly distributed.
I basically want to get rid of the Math.random().
(which will also allow me to get rid of the cast)
for(int i =0;i<NUM_STARS;i++)
{
tmpAint = star[i];
tmpAint[X] = (int)(Math.random()*SCREEN_WIDTH);
tmpAint[Y] = (int)(Math.random()*SCREEN_HEIGHT);
tmpAint[COLOR]=STAR_MIN_BRIGHTNESS + (int)(Math.random()*(255-STAR_MIN_BRIGHTNESS));
}
So, how can I use the iteration number to generate a well distributed set of X/Y positions.
I've tried something like this :-
tmpAint[X] = (i*<a large prime>)%SCREEN_WIDTH;
but it didn't work quite how I expected; and I can't be arsed thinking about why it didn't work :D
So, can any1 offer a realy clever solution?
[1223 byte] By [
Abusea] at [2007-9-29 8:46:39]

I'm in the middle of something else so this is not a complete solution (and I have no idea if the byte code will be smaller). You can probably multiply the x coord by the y coord and shift the values to get a decent random color as well?
int tmpHashCode = (new Object()).hashCode();
for (int i = 0; i < NUM_STARS; i++) {
tmpAint = star;
tmpAint[X] = (tmpHashCode * i + SCREEN_WIDTH) & Integer.MAX_VALUE >> 22;
tmpAint[Y] = (tmpHashCode * i + SCREEN_HEIGHT) & Integer.MAX_VALUE >> 22;
tmpAint[COLOR] = STAR_MIN_BRIGHTNESS + (int) (Math.random() * (255 - STAR_MIN_BRIGHTNESS));
}
I've gone with this :-
for(int i =0;i<NUM_STARS;i++)
{
tmpAint = star[i];
tmpAint[X] = (i*i*66571)%SCREEN_WIDTH;
tmpAint[Y] = (i*66931)%SCREEN_HEIGHT;
tmpAint[COLOR]= STAR_MIN_BRIGHTNESS + ((i*(255-STAR_MIN_BRIGHTNESS))/NUM_STARS);
}
The square(i*i) realy helps break up any patterns that can emerge from the parralax scrolling of the stars.
I realised the Star brightness doesn't need to be randomly distributed, so i've gone for a simple linear distribution.
The brightness is used for calculating the parralax scroll, so a linear distribution actually helps maintain an even movement.>
Abusea at 2007-7-14 22:27:08 >
