lundi 27 mars 2017

Dice roll with a 50% chance of rolling 6

So I made this method of rolling a dice 100 times with a 50% chance of rolling 6. Basic idea is that there are 50% odd numbers and 50% even numbers between 1 and 6, so if an even number is rolled, system prints 6 else it prints a random number between 1 and 5. Do you think this is correct?

public static void printDiceRolls(Random randGenerator) {
    for (int i=0;i<30;i++) {
        int temp;        
        temp=randGenerator.nextInt(6)+1;
        if (temp%2==0) {
            temp=6;
        }
        else
            temp=randGenerator.nextInt(5)+1;
        System.out.print(" "+temp+" ");
    }
}




Aucun commentaire:

Enregistrer un commentaire