|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object
|
+--java.util.Random
|
+--acm.util.RandomGenerator
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:
nextInt
and nextDouble to simplify choosing numbers in
a specific range.
nextBoolean is overloaded to allow
the specification of a probability.
nextColor that
generates a random opaque color.
| Constructor Summary | |
RandomGenerator()
Creates a new random generator initialized to an unpredictable starting point. |
|
| Method Summary | |
static RandomGenerator |
getInstance()
This method 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 |
public RandomGenerator()
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.
| Method Detail |
public static RandomGenerator getInstance()
RandomGenerator instance that can
be shared among several classes.RandomGenerator objectpublic boolean nextBoolean()
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.nextBoolean in class Randompublic boolean nextBoolean(double p)
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";
p - A value between 0 (impossible) and 1 (certain) indicating the probabilitytrue with probability ppublic Color nextColor()
Color whose components are chosen uniformly
in the 0-255 range.Color
public double nextDouble(double low,
double high)
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);
low - The low end of the rangehigh - The high end of the rangedouble value d in the range low ≤ d < highpublic int nextInt(int n)
n-1, inclusive.
This method is in modern implementations of the Random class,
but is missing from JDK 1.1.nextInt in class Random
public int nextInt(int low,
int high)
rgen.nextInt(1, 6);
or a random decimal digit by calling
rgen.nextInt(0, 9);
low - The low end of the rangehigh - The high end of the rangeint between low and high, inclusive
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||