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

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) ;

}

}

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) ;

}

}

## Comments

Assignment Expert14.03.14, 14:50Dear sarah

You're welcome. We are glad to be helpful. If you really liked our service please press like-button beside answer field. Thank you!

sarah12.03.14, 20:03thanks

## Leave a comment