mercredi 25 novembre 2020

Why isn't my java dice program matching actual probability distributions?

I made a really simple dice rolling method and figured there wouldn't be any issues, but when I test it the probability doesn't match real distributions. Is there something wrong in my test or the math.random function that causes this?

public class Dice {
    public static void main(String arg[]) {
        int[] guy = {0,0,0,0,0,0, 0,0,0,0,0,0,0,0};
        for(int i = 0; i< 1000000; i++) {
            guy[rolldice()] = guy[rolldice()] + 1;
            
        }
        for(int j = 0; j< guy.length; j++) {
            System.out.println(j + "Number times is " + guy[j]);
        }
        
    }
    
    public static int rolldice() {
        int die1 = (int)(Math.random()*6) + 1;
        int die2 = (int)(Math.random()*6) + 1;
        //System.out.println("The first die comes up " + die1);
        //System.out.println("The second die comes up " + die2);
        //System.out.println("Your total roll is " + (die1 + die2));

        return die1 + die2;
    }

}

The output is the following:

0Number times is 0
1Number times is 0
2Number times is 91530
3Number times is 91522
4Number times is 91528
5Number times is 91521
6Number times is 91523
7Number times is 91525
8Number times is 91529
9Number times is 91526
10Number times is 91527
11Number times is 91525
12Number times is 91521
13Number times is 0



Aucun commentaire:

Enregistrer un commentaire