samedi 24 janvier 2015

Random Number; 1 or 2?

How do I get the ints 1 OR 2 with either the random class or the Math class? This is what I have and I am only getting the value of 2.



public static void flip(Random r){
int heads = 0;
int totalFlips = 0;
Random number = new Random();
int randomNumber;
randomNumber = number.nextInt(2) + 1;
do{
if(randomNumber == 1){
System.out.println("heads");
heads++;
totalFlips++;
}
else{
System.out.println("tails");
totalFlips++;
}
}while(heads < 3);

if(heads == 3){
System.out.print("3 heads in a row after " +totalFlips + "flips");
}
}

Aucun commentaire:

Enregistrer un commentaire