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]
# 1
In these cases, use the Random class. It has a method called nextInt that takes the bounds as a parameter. I tested it against using Math.random(), and using the Random class is significantly faster.-JBoeing
jboeinga at 2007-7-14 22:27:08 > top of Java-index,Other Topics,Java Game Development...
# 2
read the title.... *size optimising* <_<I know about the Random class, im using Math.random() because it requires less byte code.What I want to do is remove the use of random all together, and use a simple arithmetic cludge.
Abusea at 2007-7-14 22:27:08 > top of Java-index,Other Topics,Java Game Development...
# 3
oooooh, ok, sorry about that.
jboeinga at 2007-7-14 22:27:08 > top of Java-index,Other Topics,Java Game Development...
# 4

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));

}

Heinemana at 2007-7-14 22:27:08 > top of Java-index,Other Topics,Java Game Development...
# 5
what if you used something similar to your i * bigLong % bounds.Offset one of them like using (i + 0xBB) * bigLong % bounds. That way the x,y pairs at least aren't always the same.
jboeinga at 2007-7-14 22:27:08 > top of Java-index,Other Topics,Java Game Development...
# 6

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 > top of Java-index,Other Topics,Java Game Development...
# 7
woo for creating pseudo-random number generators. Using the decimal part of e^(i*prime), then multiplying by 10000 then modding would probably give you more randomness, but that might be overkill hehe.
Malohkana at 2007-7-14 22:27:08 > top of Java-index,Other Topics,Java Game Development...