vendredi 6 mai 2016

Can i create different random numbers in one for loop in java [duplicate]

This question already has an answer here:

so i am running a for loop, and inside that for loop i have a int which made to be a random int. So my question is can i create a different random number for each time the loop runs. Right now i can create a random number but that random number is the same for the entire for loop. Here is a sample of the code that i have written. If anyone can help, that would be much appreciated :)

for(int p=0;p<5;p++)
    {
        a=(int)Math.random()*8;
        Asteroid.add(new MovingPic(rock,50,p*a+1,p*50,1,1));
    }

a is an int initialized outside of the method in the global space. So if a was randomized for each time the loop runs then the (p*a+1) would have a different value each time which is what i am trying to do but cant figure out how to.




Aucun commentaire:

Enregistrer un commentaire