import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; /** * Roll lots and lots of dice. Compute the relative frequency of * various combinations. * * @author Yvonne Palm * @author Samuel A. Rebelsky (editor) */ public class RelativeFrequency{ // One hundred asterisks. public static final String asterisks = "********************************************************************************************************************************************************************************************************"; public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out, true); out.print("Enter # of dice: ");out.flush(); int n = Integer.parseInt(in.readLine()); out.print("Enter # of times to throw the dice: ");out.flush(); int m = Integer.parseInt(in.readLine()); //Create an array to tally up the number of times each sum is obtained int[] sums = new int[(6 * n + 1)]; //start dice throwing, saving the sum in each case in sums. for (int i = 0; i <= m; i++){ sums[sum(n)]++; }//for //calculate percentages for each sum in sums. for (int i = n; i <= 6 * n; i++){ int freq = 200 * sums[i] / m; out.println(i + ": " + asterisks.substring(0,freq)); } }//main //to get the sum of the values on the dice public static int sum(int n){ int x = 0; for (int i = 1; i<=n; i++) { x = x + (int)(((6 * Math.random()) )+ 1); }//for return x; }//sum }//RelativeFrequency /* // create the random numbers that show up on the face of each die int ran = (int)(((6 * Math.random()) )+ 1); //an array of the possible sums abtained int[] sums = new int[(6*n - n + 1)]; // each position in sums has the sum of occurrences of equal sums of n random numbers int sum = ran * n; for (int i = 0; i <= 6*n; i++){ if (i == (sum - n)) sums[i] = sums[i]+1; }//for //To print the histogram for (int i = n; i <= 6*n; i++){ out.println + " : " + sums[i - n]); }//for */