Java Roll Dice 10,000 Times with Algorithm and Source Code

How to make a dice roll program in Java?

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 roll dice 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. 

Algorithm:

  1. import java package
    1. import java.io.*;
    2. import java.util.*;
  2. Create class Rand
  3. Create main function
    1. public static void main(String ar[])
  4. Declare variable
    1. int d = 0;
  5. for( int i = 0; i <= 100; i++)
    1. Create object
      1. Random r = new Random();
    2. int d1 = r.nextInt(6) + 1;
    3. compare: if( d1 == d2)
      1. increment d++;
  6. Print The number of times the two roll dice have the same number is

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 roll dice have same number is "+d);
}
}

Sample output :

Java Roll Dice 1,000 Times | dice roller | dice roll | dice roller online | dice roller dnd

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

Recommended Post:

Find the solution to the salesforce Question and many more

TAGS:

dice roller, dice roller dnd, dice roller online, dice roller calculator, dice roller java, dice roller online free, dice roller average, dice roller multiple

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



0 thoughts on “Java Roll Dice 10,000 Times with Algorithm and Source Code”

Leave a Comment