samedi 25 juin 2016

Random numbers and while loop issues

I have to generate two random numbers for 52000 instances in an ArrayList, attack 1 and attack 2, where attack 1 must be greater than attack 2, so i have to put them in the list. This is my sample code:

do{
            atk_p1 = (int) (Math.random() * (5000-500+1)+500);
            atk_p2 = (int) (Math.random() * (5000-500+1)+500);
}while(atk_p1 <= atk_p2);
data.add(String.valueOf(atk_p1));
data.add(String.valueOf(atk_p2));

My problem is that the above code sometimes works (thus atk_p1 is greater than atk_p2) but other times not (thus atk_p2 is greater than atk_p1). I'm trying to solve the issue. Thank you for your help.




Aucun commentaire:

Enregistrer un commentaire