/**
* Demonstrate arrays in Java: solve Problem 7.17 of Deitel & Deitel
*
* @author (Michael Branicky), 10/03/06, 09/26/2007
*/
public class DiceStats 
{
   public static int rollDie() {
     return (int) Math.floor(Math.random()*6)+1;
   }

   public static void main(String args[])
   {
     int i;

     final int ROLLS = 36000;  // constant, "magic number" number of rolls
 
     int observed_count [] = new int [13];
     double freq [] = new double [13];

     // counts of outcomes from output of DieSums.java
     int count [] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };

     // Roll the dice!
     System.out.println("Generating "+ROLLS+" dice rolls ...");
     for (i=0; i<ROLLS; i++) {
       // NOTE: sum is local to this for loop!
       int sum;
       
       // roll two die and compute sum
       sum = rollDie() + rollDie();

       // update appropriate counter
       observed_count[sum]++;
     }

     // only sums from 2 through 12 are possible
     for (i=2; i<=12; i++) {
       // compute the observed frequencies of each sum
       freq[i] = observed_count[i]/((double) ROLLS);
       System.out.print("Observed frequency of "+i+": "+freq[i]);
       // compute the probability of each of the 36 possible sums
       System.out.println(" (prob. = "+count[i]/36.+")");
     }
  }
}
