I am a beginner and have currently started working on a game for Android which uses a particle swarm optimization algorithm. I am now trying to optimize my code a little and i have quite a lot of Math.random() in for-loops which is running almost all the time. So i was thinking of a way to get around and skip all the Math.random() calls.
By using a method like this:
float random[] = new float[100];
static int randomIndex=0;
private float myRandom(){
if(randomIndex >= 99)
randomIndex = 0;
else
randomIndex = randomIndex+1;
return random[randomIndex];
}
...and also do this one time when the activity starts:
for (int i=0; i< 100; i++)
random[i]=(float) Math.random();
My question is if this will be better (faster) than using Math.random()? Does anyone have a better suggestion how to do?
I also wonder if anyone know any good site where i can read more about how to write efficient java/android code. I'm afraid i kind of suck on it.