Question #17752

Probability: Rolling 6 sided dice can teach us about probability.
Allow the user to choose a number of rolls for 2 six sided dice. Use random number generator to produce the resulting rolls.

Add the dice together and keep track of how many of each of the sums are rolled. Print out a frequency distribution table and then a frequency histogram

Expert's answer

import java.util.Arrays;
import java.util.Scanner;

public class Dice {

public static void main(String[] args) {
& @SuppressWarnings("resource")
& Scanner input= new Scanner(System.in);
& System.out.print("Choose a number of rolls for 2 six sided dice: ");
& int in=input.nextInt();
& int[] summarr = new int [12];
& Arrays.fill(summarr,0);
& for (int i = 0; i<in; i++) {
& summarr[throwDice() + throwDice() - 1]++;
& }
& for (int i = 2; i<13; i++) {
& System.out.printf("%2d& %d",i,summarr[i-1]);
& System.out.println();
&
& }
}
public static int throwDice () {
& return ((int) (Math.random() *6) + 1) ;
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS