Roll Die - frequency counter


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

0 Comment:

Post a Comment

A Hacker Speaks One Line Description Avatar Logo Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

Blogger Buster LogoSmashing Logo© A Hacker Speaks.