Question: For the assignment you need to complete two functions: public static int rollDice() : The method simulates the roll of two 6-sided dice. It does
For the assignment you need to complete two functions:
public static int rollDice(): The method simulates the roll of two 6-sided dice. It does this by producing two independently chosen psuedorandom numbers in the range [1, 6] (e.g. greater than or equal to 1 and less than or equal to 6) and returning the sum of the two numbers. So the value returned will be in the range [2, 12] (i.e. greater than or equal to 2 and less than or equal to 12). Note that two numbers in the range [1, 6] should be generated and then added. Generating a single number in the range [2, 12] doesn't produce the same probabilities and would be incorrect.
public static void main(String[] args): The main method has several variables defined at the top. r is the total roll of the two dice desired. So r is an integer and would have a value in the range [2, 12]. rounds is the number of times we are aiming to get the roll of r. In the template rounds is set to 1000, which means we are aiming to get 1000 rolls with sum equal to r. You will modify the main method so that it rolls two dice, using a call to rollDice(). If the roll obtained is r, then it will count that toward the total. When a total of rounds rolls with value equal to r is obtained, the main method stops the loop and indicates how many rolls it took overall to achieve rounds number of rolls with the total r.
The following is sample output from my solution for each possible value of r using a value of 1000 for rounds. This means that the output you see below comes from running the program 12 different times, once for each possible value of r in the range [2, 12]. Please remember that because there is randomness involved, the precise counts you will see will likely differ from mine. But they should be in the same order of magnitude (e.g. roughly 5000 as opposed to 50000 or 500). You aren't required to submit a program that would show all possible runs. I've simply produced all possible runs of my solution for rounds = 1000 so that you can see what order of magnitude a correct solution would produce.
Run for r = 2: It took 37626 rounds to reach 1000 rolls of 2.
Run for r = 3: It took 18062 rounds to reach 1000 rolls of 3.
Run for r = 4: It took 12879 rounds to reach 1000 rolls of 4.
Run for r = 5: It took 8849 rounds to reach 1000 rolls of 5.
Run for r = 6: It took 7086 rounds to reach 1000 rolls of 6.
Run for r = 7: It took 5987 rounds to reach 1000 rolls of 7.
Run for r = 8: It took 7283 rounds to reach 1000 rolls of 8.
Run for r = 9: It took 8982 rounds to reach 1000 rolls of 9.
Run for r = 10: It took 11464 rounds to reach 1000 rolls of 10.
Run for r = 11: It took 18284 rounds to reach 1000 rolls of 11.
Run for r = 12: It took 35996 rounds to reach 1000 rolls of 12.
public class DiceProb {
public static void main(String[] args) {
int rounds = 1000;
int r = 5;
int total = 0;
// Declare extra variables here
// Roll two 6-sided dice as many times as necessary to get a total of r
// rounds number of times
// E.g. if r is 5 and rounds is 1000, we want to roll two dice as many times
// as needed until we've seen a sum of 5 precisely 1000 times
// The rolling of the dice will be doing using a call to rollDice
// Don't duplicate that code in the main
System.out.println("It took " + total + " rounds to reach " + rounds + " rolls of " + r + ".");
}
public static int rollDice() {
// A stub
// Re-write this to simulate rolling two 6-sided die and return the sum of the rolls
return 2;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
