Question

In: Computer Science

DESCRIPTION Complete the given program to simulate the roll of two 6-sided dice and count the...

DESCRIPTION

Complete the given program to simulate the roll of two 6-sided dice and count the number of rolls of each possible roll value (2 to 12). The number of rolls will be input by the user.

Count the rolls of each type and then "draw" a histogram by printing a "*" for each roll of each type. The shape of the histogram will follow the "probability distribution" for large numbers of rolls, i.e., it will show more rolls for the more likely roll values (e.g., 6, 7, 8) and fewer for the less likely roll values (e.g., 2, 3, 11, 12).

You can store the counts of the rolls in an array of size 13. This will allow you to store the counts of rolls 2-12 (with the 0 and 1 spots left unused and ignored for this program). Then, after each roll, just add 1 to the corresponding spot in the array.

Notes:

* make sure to generate each roll by generating two random numbers between 1 and 6 and adding them together. this will best simulate the rolling of two 6-sided dice.

* to line up the labels of the values of the histogram, you can use System.out.printf("%2d:", i); where i is the label. this will print each value using 2 spaces so the single-digit numbers (2, 3, 4, etc.) take the same width as the double-digit numbers (10, 11, 12)

* again, the seed value is being used to make the randomness more predictable for testing in Mimir. you can input any seed number you want when you are testing the code yourself.

Sample output:

Enter seed value for random number generator:
>6
How many rolls?
>80
2: ***
3: *****
4: ******
5: *******
6: *********
7: ***************
8: **************
9: ********
10: *****
11: ********
12:

Given Code:

/*
* simulate the rolls of two 6-sided dice
* "draw" a histogram of the rolled values
*/

import java.util.Scanner;
import java.util.Random;

public class CountRolls {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter seed value for random number generator:"); //used by Mimir for testing
int seed = scnr.nextInt();
Random rand = new Random(seed);
System.out.println("How many rolls?"); //get total number of rolls from input
int numRolls = scnr.nextInt();

//complete the program
  

} //end main
} //end CountRolls

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

/*
 * simulate the rolls of two 6-sided dice
 * "draw" a histogram of the rolled values
 */

import java.util.Scanner;
import java.util.Random;

public class CountRolls {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        System.out.println("Enter seed value for random number generator:"); //used by Mimir for testing
        int seed = scnr.nextInt();
        Random rand = new Random(seed);
        System.out.println("How many rolls?"); //get total number of rolls from input
        int numRolls = scnr.nextInt();

        int freq[] = new int[13];

        for (int roll = 1; roll <= numRolls; roll++) {

            int roll1 = rand.nextInt(6) + 1;
            int roll2 = rand.nextInt(6) + 1;
            freq[roll1 + roll2] += 1;
        }

        for (int index = 1; index < freq.length; index++) {

            System.out.printf("%2d:", index);

            for (int star = 1; star <= freq[index]; star++) {
                System.out.print("*");
            }

            System.out.println();
        }

//complete the program


    } //end main
} //end C

=======================================================================


Related Solutions

You roll two 6-sided dice numbered 1 through 6. Let A be the event that the...
You roll two 6-sided dice numbered 1 through 6. Let A be the event that the first die shows the number 3, let B be the event that the second die shows a 5, and let E be the event that the sum of the two numbers showing is even. Compute P(A)and P(B)and then compute P(AlB). What does this tell you about events A and B?Hint: Remember that the sample space has 36 outcomes! Compute P(ElA)and compute P(E). What does...
Suppose you roll two 6 sided dice, letting X be the sum of the numbers shown...
Suppose you roll two 6 sided dice, letting X be the sum of the numbers shown on the dice and Y be the number of dice that show an odd number. a) Find the joint pmf of <X,Y> b) Find the marginals pmf's for both variables. c) Are X and Y independent?
Suppose you roll two 6 sided dice, letting X be the sum of the numbers shown...
Suppose you roll two 6 sided dice, letting X be the sum of the numbers shown on the dice and Y be the number of dice that show an odd number. a) Find the joint pmf of <X,Y> b) Find the marginals pmf's for both variables. c) Are X and Y independent?
Suppose you roll two 6-sided dice, letting X be the sum of the numbers shown on...
Suppose you roll two 6-sided dice, letting X be the sum of the numbers shown on the dice and Y be the number of dice that show an odd number. a) Find the joint pmf of <X,Y> b) Find the maringals pmf's for both variables. c) Are X and Y independent?
Find the conditional​ probability, in a single roll of two fair​ 6-sided dice, that neither die...
Find the conditional​ probability, in a single roll of two fair​ 6-sided dice, that neither die is a three​,given that the sum is greater than 7.
If I roll two 6 sided dice, one yellow and one clear, what are the possible...
If I roll two 6 sided dice, one yellow and one clear, what are the possible microstates, and what is the Gibbs entropy at 298.15 K? What is the entropy if the dice are indistinguishable? Calculate the entropy if one can only measure the sum of the dice instead of their individual values.
Consider some 8-sided dice. Roll two of these dice. Let X be the minimum of the...
Consider some 8-sided dice. Roll two of these dice. Let X be the minimum of the two values that appear. Let Y denote the maximum.   a) Find the joint mass p_X,Y (x,y). b) Compute p_X│Y (x│y) in all cases. Express your final answer in terms of a table.
So, roll a fair 6-sided dice once, and if the result is 1,2,3, or 4 then...
So, roll a fair 6-sided dice once, and if the result is 1,2,3, or 4 then toss a fair coin 3 times. If the first result is 5, 6, then toss a fair coin until two tails show up. Then, what is the expected value of number of heads? PLZ help me with this!!! THX soooooo much!
You roll two, fair (i.e., not weighted) 6-sided dice once. What is the probability that: 3a....
You roll two, fair (i.e., not weighted) 6-sided dice once. What is the probability that: 3a. The sum is 11 or more? (1 Point) 3b. The sum is 7? (1 Point) 3c. The sum is 6? (1 Point) 3d. Thesumis6or8? (1Point) 3e. The sum is less than 4? (1 Point) 3f. The sum is something other than 2, 7, or 11? (2 Points)
Suppose you roll, two 6-sided dice (refer back to the sample space in the sample space...
Suppose you roll, two 6-sided dice (refer back to the sample space in the sample space notes). Write any probability as a decimal to three place values and the odds using a colon. Determine the following: a. the probability that you roll a sum of seven (7) is . b. The odds for rolling a sum of four (4) is . c. The odds against the numbers on both dice being the same is .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT