Java roll dice 10000 times with algorithm and source code

Java roll dice 10000 times with algorithm and source code

Week 5

a)Aim: To Write a java program to make rolling a pair of dice 10,000 times and counts the number of times doubles of are rolled for each different pair of doubles. Hint: Math.random()

Description :

For this program, no inputs are required. The output of this program is number of times two dice show similar number when rolled for 10,000 times.

Here we use, random() method defined in Math class. It picks one number from 1 to 6. The randomly selected numbers are compared, if they are equal, the count increases else it continues. 

Source code :

import java.io.*;

import java.util.*;

class Rand

{

            public static void main(String ar[])

            {

                        int d=0;

                        for(int i=0;i<=100;i++)

                        {

                                    Random r=new Random();

                                    int d1=r.nextInt(6)+1;

                                    int d2=r.nextInt(6)+1;

                                    if(d1==d2)

                                      d++;

                        }

                        System.out.println(“The number of times the two dice have same number is  “+d);

            }

}

Sample output :

Java roll dice 10000 times with algorithm and source code

The number of times the two dice have same number is 3437

Java roll dice 10000 times with algorithm and source code

https://en.wikipedia.org/wiki/Dice_10000

Leave a Comment

Your email address will not be published. Required fields are marked *