Question:
Java program to calculate the frequency of the sum of the rolling of a pair of dice.
Code:
public class RollDice { public static void main(String[] args) { int frequency[] = new int[13]; int total=0; for (int roll = 1; roll <= 5000; roll++) { total = 1 + (int) (Math.random() * 6); total = total + 1 + (int) (Math.random() * 6); frequency[total] = frequency[total] + 1; } System.out.println("Total\tFrequency"); for(int n=2;n<frequency.length;n++){ System.out.println(n + "\t" + frequency[n]); } } }
Output:
$ java RollDice Total Frequency 2 141 3 275 4 424 5 579 6 674 7 821 8 705 9 560 10 410 11 268 12 143