Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

DESCRIPTION Create a program to perform a random experiment. The experiment is to roll two six-sided dice until a 7 is rolled (a 1 and

DESCRIPTION

Create a program to perform a random experiment. The experiment is to roll two six-sided dice until a 7 is rolled (a 1 and a 6, a 3 and a 4, a 5 and a 2, etc.). Count the number of rolls it takes to roll a 7 (which roll number is the 7 rolled). In this program, ask the user for a number of trials. In EACH trial, roll until a 7 is reached and print out the number of rolls it takes. So we are performing the experiment multiple times.

Additionally, keep track of the total number of rolls over all the trials. After all the trials are completed, print out the average number of rolls, over all the trials, to reach a 7. This experiment follows a probability distribution known as the Geometric Distribution. For a large number of trials, the average over the trials should approach 6 because the probability of rolling a 7 is 1/6.

Note: to make the program gradeable by Mimir, the random numbers need to be generated using a given "seed" value. The given code has already created the Random class object that you can use to make your rolls.

Hint: to "roll" a die, you can use r.nextInt(6)+1 to generate a random number between 1 and 6. If you do this twice, it is simulating rolling two dice that you can add up to get the sum of the dice. So each roll will consist of generating two new random values and adding them up.

Sample output of the program:

Enter the seed value for the random number generator: 1 Enter the number of trials: 12 Trial #1: 10 rolls to roll a 7. Trial #2: 5 rolls to roll a 7. Trial #3: 1 rolls to roll a 7. Trial #4: 4 rolls to roll a 7. Trial #5: 7 rolls to roll a 7. Trial #6: 9 rolls to roll a 7. Trial #7: 1 rolls to roll a 7. Trial #8: 5 rolls to roll a 7. Trial #9: 14 rolls to roll a 7. Trial #10: 4 rolls to roll a 7. Trial #11: 6 rolls to roll a 7. Trial #12: 3 rolls to roll a 7. Average 5.75 rolls to roll a 7 over 12 trials.

Starter code:-

/* * a program that performs a given number of trials of a random experiment * on each trial of the experiment, simulate the rolling of two six-sided dice until a 7 is rolled * note: the random number generator has already been created using a given seed value * for any given seed, the sequence of roll values will be the same */ import java.util.*; public class Geometric { public static void main(String[] args) { //create the Scanner and the Random number generator using a given seed Scanner scnr = new Scanner(System.in); System.out.println("Enter the seed value for the random number generator:"); int seed = scnr.nextInt(); Random r = new Random(seed); //TODO: complete the program } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions