When rolling three dice, the possible outcomes for the total of a roll range fro
ID: 3572902 • Letter: W
Question
When rolling three dice, the possible outcomes for the total of a roll range from 3 to 18. That is three dice all showing one dot to all showing six dots. The distribution of the roll outcomes can be simulated by rolling the dice many times. Write a Java program that performs that simulation. Prompt the user to enter the number of rolls to simulate and then report how many of each total was rolled. A sample run of the program might appear as follows: Enter the number of rolls to simulate: 5000 The total number of 3s is: 437 The total number of 4s is: 621 The total number of 18s is: 421Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class DiceSimulation {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int count[] = new int[19];
for(int i=0;i<19;i++)
{
count[i]=0;
}
System.out.print("Enter the number of rolls to simulate:");
int r = scan.nextInt();
for(int i=0;i<r;i++)
{
int tmp = rand.nextInt(6)+1+rand.nextInt(6)+1+rand.nextInt(6)+1;
count[tmp]++;
}
for(int i=3;i<19;i++)
{
System.out.println("The total number of "+i+"s is:"+count[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.