home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / src / COM / objectspace / jgl / Randomizer.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  5.0 KB  |  209 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package COM.objectspace.jgl;
  5.  
  6. import java.util.Random;
  7.  
  8. /**
  9.  * An easy-to-use random number generator.
  10.  * <p>
  11.  * @version 2.0.2
  12.  * @author ObjectSpace, Inc.
  13.  */
  14.  
  15. public class Randomizer extends java.util.Random
  16.   {
  17.   static Randomizer random = new Randomizer();
  18.  
  19.   /**
  20.    * Initializes the generator using a seed based on the current time.
  21.   **/
  22.   public Randomizer()
  23.     {
  24.     }
  25.  
  26.   /**
  27.    * Initializes the generator using a specific seed; useful for generating
  28.    * a repeatable stream of random numbers.
  29.    * @param seed The initial seed.
  30.    * @see java.util.Random#setSeed
  31.   **/
  32.   public Randomizer( long seed )
  33.     {
  34.     super( seed );
  35.     }
  36.  
  37.   /**
  38.    * Generates an int value between 1 and the given limit.
  39.    * @param hi The upper bound.
  40.    * @return An integer value.
  41.    * @see java.util.Random#nextInt
  42.   **/
  43.   public int nextInt( int hi )
  44.     {
  45.     return nextInt( 1, hi );
  46.     }
  47.  
  48.   /**
  49.    * Generates an int value between the given limits.
  50.    * @param lo The lower bound.
  51.    * @param hi The upper bound.
  52.    * @return An integer value.
  53.    * @see java.util.Random#nextInt
  54.   **/
  55.   public int nextInt( int lo, int hi )
  56.     {
  57.     if ( lo > hi )
  58.       throw new InvalidOperationException( "invalid range: " + lo + " > " + hi );
  59.     return ( Math.abs( super.nextInt() ) % ( hi - lo + 1 ) ) + lo;
  60.     }
  61.  
  62.   /**
  63.    * Generates a long value between 1 and the given limit.
  64.    * @param hi The upper bound.
  65.    * @return A long value.
  66.    * @see java.util.Random#nextLong
  67.   **/
  68.   public long nextLong( long hi )
  69.     {
  70.     return nextLong( 1, hi );
  71.     }
  72.  
  73.   /**
  74.    * Generates a long value between the given limits.
  75.    * @param lo The lower bound.
  76.    * @param hi The upper bound.
  77.    * @return A long integer value.
  78.    * @see java.util.Random#nextLong
  79.   **/
  80.   public long nextLong( long lo, long hi )
  81.     {
  82.     if ( lo > hi )
  83.       throw new InvalidOperationException( "invalid range: " + lo + " > " + hi );
  84.     return ( Math.abs( super.nextLong() ) % ( hi - lo + 1 ) ) + lo;
  85.     }
  86.  
  87.   /**
  88.    * Generates a float value between 1.0 and the given limit.
  89.    * @param hi The upper bound.
  90.    * @return A float value.
  91.    * @see java.util.Random#nextFloat
  92.   **/
  93.   public float nextFloat( float hi )
  94.     {
  95.     return nextFloat( 1, hi );
  96.     }
  97.  
  98.   /**
  99.    * Generates a float value between the given limits.
  100.    * @param lo The lower bound.
  101.    * @param hi The upper bound.
  102.    * @return A float value.
  103.    * @see java.util.Random#nextFloat
  104.   **/
  105.   public float nextFloat( float lo, float hi )
  106.     {
  107.     if ( lo > hi )
  108.       throw new InvalidOperationException( "invalid range: " + lo + " > " + hi );
  109.     return ( Math.abs( super.nextFloat() ) % ( hi - lo + 1 ) ) + lo;
  110.     }
  111.  
  112.   /**
  113.    * Generates a double value between 1.0 and the given limit.
  114.    * @param hi The upper bound.
  115.    * @return A double value.
  116.    * @see java.util.Random#nextDouble
  117.   **/
  118.   public double nextDouble( double hi )
  119.     {
  120.     return nextDouble( 1, hi );
  121.     }
  122.  
  123.   /**
  124.    * Generates a double value between the given limits.
  125.    * @param lo The lower bound.
  126.    * @param hi The upper bound.
  127.    * @return A double value.
  128.    * @see java.util.Random#nextDouble
  129.   **/
  130.   public double nextDouble( double lo, double hi )
  131.     {
  132.     if ( lo > hi )
  133.       throw new InvalidOperationException( "invalid range: " + lo + " > " + hi );
  134.     return ( Math.abs( super.nextDouble() ) % ( hi - lo + 1 ) ) + lo;
  135.     }
  136.  
  137.   /**
  138.    * Generate a random number using the default generator.
  139.    * @see #nextInt
  140.   **/
  141.   public static int getInt( int hi )
  142.     {
  143.     return getInt( 1, hi );
  144.     }
  145.  
  146.   /**
  147.    * Generate a random number using the default generator.
  148.    * @see #nextInt
  149.   **/
  150.   public static int getInt( int lo, int hi )
  151.     {
  152.     return random.nextInt( lo, hi );
  153.     }
  154.  
  155.   /**
  156.    * Generate a random number using the default generator.
  157.    * @see #nextLong
  158.   **/
  159.   public static long getLong( long hi )
  160.     {
  161.     return getLong( 1, hi );
  162.     }
  163.  
  164.   /**
  165.    * Generate a random number using the default generator.
  166.    * @see #nextLong
  167.   **/
  168.   public static long getLong( long lo, long hi )
  169.     {
  170.     return random.nextLong( lo, hi );
  171.     }
  172.  
  173.   /**
  174.    * Generate a random number using the default generator.
  175.    * @see #nextFloat
  176.   **/
  177.   public static float getFloat( float hi )
  178.     {
  179.     return getFloat( 1, hi );
  180.     }
  181.  
  182.   /**
  183.    * Generate a random number using the default generator.
  184.    * @see #nextFloat
  185.   **/
  186.   public static float getFloat( float lo, float hi )
  187.     {
  188.     return random.nextFloat( lo, hi );
  189.     }
  190.  
  191.   /**
  192.    * Generate a random number using the default generator.
  193.    * @see #nextDouble
  194.   **/
  195.   public static double getDouble( double hi )
  196.     {
  197.     return getDouble( 1, hi );
  198.     }
  199.  
  200.   /**
  201.    * Generate a random number using the default generator.
  202.    * @see #nextDouble
  203.   **/
  204.   public static double getDouble( double lo, double hi )
  205.     {
  206.     return random.nextDouble( lo, hi );
  207.     }
  208.   }
  209.