acm.util
Class RandomGenerator

java.lang.Object
  extended by java.util.Random
      extended by acm.util.RandomGenerator
All Implemented Interfaces:
Serializable

public class RandomGenerator
extends Random

This class implements a simple random number generator that allows clients to generate pseudorandom integers, doubles, booleans, and colors. To use it, the first step is to declare an instance variable to hold the random generator as follows:


      private RandomGenerator rgen = RandomGenerator.getInstance();
 

By default, the RandomGenerator object is initialized to begin at an unpredictable point in a pseudorandom sequence. During debugging, it is often useful to set the internal seed for the random generator explicitly so that it always returns the same sequence. To do so, you need to invoke the setSeed method.

The RandomGenerator object returned by getInstance is shared across all classes in an application. Using this shared instance of the generator is preferable to allocating new instances of RandomGenerator. If you create several random generators in succession, they will typically generate the same sequence of values.

See Also:
Serialized Form

Constructor Summary
RandomGenerator()
          Creates a new random generator.
 
Method Summary
static RandomGenerator getInstance()
          Returns a RandomGenerator instance that can be shared among several classes.
 boolean nextBoolean()
          Returns a random boolean value that is true or false with equal probability.
 boolean nextBoolean(double p)
          Returns a random boolean value with the specified probability.
 Color nextColor()
          Returns a random opaque color whose components are chosen uniformly in the 0-255 range.
 double nextDouble(double low, double high)
          Returns the next random real number in the specified range.
 int nextInt(int n)
          Returns the next random integer between 0 and n-1, inclusive.
 int nextInt(int low, int high)
          Returns the next random integer in the specified range.
 
Methods inherited from class java.util.Random
next, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextLong, setSeed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomGenerator

public RandomGenerator()
Creates a new random generator. Most clients will not use the constructor directly but will instead call getInstance to obtain a RandomGenerator object that is shared by all classes in the application.

Method Detail

getInstance

public static RandomGenerator getInstance()
Returns a RandomGenerator instance that can be shared among several classes.

Returns:
A shared RandomGenerator object

nextBoolean

public boolean nextBoolean()
Returns a random boolean value that is true or false with equal probability. This method is in modern implementations of the Random class, but is missing from JDK 1.1.

Overrides:
nextBoolean in class Random

nextBoolean

public boolean nextBoolean(double p)
Returns a random boolean value with the specified probability. You can use this method to simulate an event that occurs with a particular probability. For example, you could simulate the result of tossing a coin like this:


      String coinFlip = rgen.nextBoolean(0.5) ? "HEADS" : "TAILS";
 

Parameters:
p - A value between 0 (impossible) and 1 (certain) indicating the probability
Returns:
The value true with probability p

nextColor

public Color nextColor()
Returns a random opaque color whose components are chosen uniformly in the 0-255 range.

Returns:
A random opaque Color

nextDouble

public double nextDouble(double low,
                         double high)
Returns the next random real number in the specified range. The resulting value is always at least low but always strictly less than high. You can use this method to generate continuous random values. For example, you can set the variables x and y to specify a random point inside the unit square as follows:


      double x = rgen.nextDouble(0.0, 1.0);
      double y = rgen.nextDouble(0.0, 1.0);
 

Parameters:
low - The low end of the range
high - The high end of the range
Returns:
A random double value d in the range lowd < high

nextInt

public int nextInt(int n)
Returns the next random integer between 0 and n-1, inclusive. This method is in modern implementations of the Random class, but is missing from JDK 1.1.

Overrides:
nextInt in class Random

nextInt

public int nextInt(int low,
                   int high)
Returns the next random integer in the specified range. For example, you can generate the roll of a six-sided die by calling


      rgen.nextInt(1, 6);
 

or a random decimal digit by calling


      rgen.nextInt(0, 9);
 

Parameters:
low - The low end of the range
high - The high end of the range
Returns:
The next random int between low and high, inclusive