acm.util
Class RandomGenerator

java.lang.Object
  |
  +--java.util.Random
        |
        +--acm.util.RandomGenerator

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();

When you then need to generate a random value, you call the appropriate method on the rgen variable.

The RandomGenerator class is actually implemented as an extension to the Random class in java.util. The new version has the following advantages over the original:


Constructor Summary
RandomGenerator()
Creates a new random generator initialized to an unpredictable starting point.
 
Method Summary
RandomGenerator getInstance()
This method returns a RandomGenerator instance that can be shared among several classes.
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 low, int high)
Returns the next random integer in the specified range.
 
Inherited Method Summary
booleannextBoolean()
Returns a random boolean that is true 50 percent of the time.
doublenextDouble()
Returns a random double d in the range 0 ≤ d < 1.
intnextInt(int n)
Returns a random integer k in the range 0 ≤ k < n.
voidsetSeed(long seed)
Sets a new starting point for the random generator sequence.
 

Constructor Detail

public RandomGenerator()

Creates a new random generator initialized to an unpredictable starting point. In almost all programs, you want to call this method once to produce a single generator object, which you then use each time you need to generate a random value. If you create several random generators in succession, they will typically generate the same sequence of values.

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.


 
Usage: RandomGenerator rgen = new RandomGenerator(); 
 
Method Detail

public static RandomGenerator getInstance()

This method returns a RandomGenerator instance that can be shared among several classes.

 
Usage: RandomGenerator rgen = RandomGenerator.getInstance(); 
Returns: A shared RandomGenerator object
 

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";


 
Usage: if (rgen.nextBoolean(p)) . . . 
Parameter: 
 A value between 0 (impossible) and 1 (certain) indicating the probability
Returns: The value true with probability p
 

public Color nextColor()

Returns a random opaque Color whose components are chosen uniformly in the 0-255 range.

 
Usage: Color color = rgen.newColor() 
Returns: A random opaque Color
 

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);


 
Usage: double d = rgen.nextDouble(low, high) 
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
 

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);


 
Usage: int k = rgen.nextInt(low, high) 
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