import java.util.Random;
public class RollDie {
public static void main(String args[]) {
Random randomNumbers = new Random(); // random number generator
int frequency[] = new int[6]; // maintains count of 1 to 6s rolled
int face; // stores most recently rolled value
// summarize results of a billion rolls of a die
for (int roll = 1; roll <= 100000000; roll++) {
face = 1 + randomNumbers.nextInt(6); // number from 1 to 6
// determine roll value 1-6 and increment appropriate counter
switch (face) {
case 1:
++frequency[0]; // increment the 1s counter
break;
case 2:
++frequency[1]; // increment the 2s counter
break;
case 3:
++frequency[2]; // increment the 3s counter
break;
case 4:
++frequency[3]; // increment the 4s counter
break;
case 5:
++frequency[4]; // increment the 5s counter
break;
case 6:
++frequency[5]; // increment the 6s counter
break; // optional at end of switch
} // end switch
} // end for
System.out.println("Face\tFrequency\tPercentage"); // output headers
for (int i = 0; i < 6; i++) {
System.out.print(i);
System.out.print("\t");
System.out.print(frequency[i]);
System.out.print("\t");
System.out.print((frequency[i] / 1000000) * 100);
System.out.println();
}
} // end main
} // end class RollDie
Roll Die - frequency counter
About this entry
You are currently reading Roll Die - frequency counter.
- Entry by
- 86.101.110.117 on 18.10.08 on 18.10.08
0 Comment: