Source of RollDie.java


  1: // Fig. 7.7: RollDie.java
  2: // Roll a six-sided die 6000 times.
  3: import java.util.Random;
  4: 
  5: public class RollDie 
  6: {
  7:    public static void main( String args[] )
  8:    {
  9:       Random randomNumbers = new Random(); // random number generator
 10:       int frequency[] = new int[ 7 ]; // array of frequency counters
 11: 
 12:       // roll die 6000 times; use die value as frequency index
 13:       for ( int roll = 1; roll <= 6000; roll++ ) 
 14:          ++frequency[ 1 + randomNumbers.nextInt( 6 ) ];  
 15: 
 16:       System.out.printf( "%s%10s\n", "Face", "Frequency" );
 17:    
 18:       // output each array element's value
 19:       for ( int face = 1; face < frequency.length; face++ )
 20:          System.out.printf( "%4d%10d\n", face, frequency[ face ] );
 21:    } // end main
 22: } // end class RollDie
 23: 
 24: 
 25: 
 26: /**************************************************************************
 27:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 28:  * Pearson Education, Inc. All Rights Reserved.                           *
 29:  *                                                                        *
 30:  * DISCLAIMER: The authors and publisher of this book have used their     *
 31:  * best efforts in preparing the book. These efforts include the          *
 32:  * development, research, and testing of the theories and programs        *
 33:  * to determine their effectiveness. The authors and publisher make       *
 34:  * no warranty of any kind, expressed or implied, with regard to these    *
 35:  * programs or to the documentation contained in these books. The authors *
 36:  * and publisher shall not be liable in any event for incidental or       *
 37:  * consequential damages in connection with, or arising out of, the       *
 38:  * furnishing, performance, or use of these programs.                     *
 39:  *************************************************************************/