Source of RandomIntegers.java


  1: // Fig. 6.7: RandomIntegers.java
  2: // Shifted and scaled random integers.
  3: import java.util.Random; // program uses class Random
  4: 
  5: public class RandomIntegers 
  6: {
  7:    public static void main( String args[] )
  8:    {      
  9:       Random randomNumbers = new Random(); // random number generator
 10:       int face; // stores each random integer generated
 11: 
 12:       // loop 20 times
 13:       for ( int counter = 1; counter <= 20; counter++ ) 
 14:       {
 15:          // pick random integer from 1 to 6
 16:          face = 1 + randomNumbers.nextInt( 6 );
 17: 
 18:          System.out.printf( "%d  ", face ); // display generated value
 19:          
 20:          // if counter is divisible by 5, start a new line of output
 21:          if ( counter % 5 == 0 )
 22:             System.out.println();
 23:       } // end for
 24:    } // end main
 25: } // end class RandomIntegers
 26: 
 27: 
 28: /**************************************************************************
 29:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 30:  * Pearson Education, Inc. All Rights Reserved.                           *
 31:  *                                                                        *
 32:  * DISCLAIMER: The authors and publisher of this book have used their     *
 33:  * best efforts in preparing the book. These efforts include the          *
 34:  * development, research, and testing of the theories and programs        *
 35:  * to determine their effectiveness. The authors and publisher make       *
 36:  * no warranty of any kind, expressed or implied, with regard to these    *
 37:  * programs or to the documentation contained in these books. The authors *
 38:  * and publisher shall not be liable in any event for incidental or       *
 39:  * consequential damages in connection with, or arising out of, the       *
 40:  * furnishing, performance, or use of these programs.                     *
 41:  *************************************************************************/