Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an application to simulate the rolling of two dice. The application should

ID: 3561338 • Letter: W

Question

Write an application to simulate the rolling of two dice. The application should use and object of class Random once to roll the first die and again to roll the second die. The sum of the two values shoulb be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum, and 2 and 12 the least frequent. There are 36 possible combinations of the two dice. your application should roll the dice 36,000,000 times. Use a one dimensional array to tally the number of times each possible sum appears.

The output is:

Sum of 2   1000306 times
Sum of 3   1998952 times
Sum of 4   2998912 times
Sum of 5   4001589 times
Sum of 6   5000651 times
Sum of 7   6000848 times
Sum of 8   4997883 times
Sum of 9   4000995 times
Sum of 10   3002157 times
Sum of 11   1997706 times
Sum of 12   1000001 times

Explanation / Answer

//represent sums, 2-12. For simplicity, use 0-12 in the array andignore 0/1
int sums[] = new sums[13];

Random rand = new Random();

for( int i = 0; i < 36000; i++ )
{
//rand.nextInt(6) returns 0-5, so add 1 to scale to a die roll.
int sum = 1 + rand.nextInt(6) + 1 + rand.nextInt(6);
sums[sum]++;
}

for( int i = 2; i <= 12; i++ )
{
System.out.println( i + ": " + sums[i] );
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote