In: Computer Science
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
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
=======================================================================