public class RandomGenerator extends Random
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.
Constructor and Description |
---|
RandomGenerator()
Creates a new random generator.
|
RandomGenerator(long seed)
Creates a new random generator, using the given seed value to influence
its random generation of values.
|
Modifier and Type | Method and Description |
---|---|
static void |
clearFixedIntegers()
Removes any previously added fixed numbers that were passed to
feedFixedIntegers.
|
static void |
feedFixedIntegers(int... numbers)
Adds a set of fixed numbers that will be returned by future calls to
nextInt.
|
static void |
feedFixedIntegers(Iterable<Integer> numbers)
Adds a set of fixed numbers that will be returned by future calls to
nextInt.
|
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. |
<T> T |
nextChoice(Iterable<T> choices)
Randomly chooses one of the values passed and returns it.
|
<T> T |
nextChoice(List<T> choices)
Randomly chooses one of the values passed and returns it.
|
<T> T |
nextChoice(T... choices)
Randomly chooses one of the values passed and returns it.
|
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()
Returns the next random integer, which can be any legal integer value.
|
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.
|
static void |
setSharedSeed(long seed)
Sets a seed value that will be used by all RandomGenerators when they are
constructed.
|
public RandomGenerator()
getInstance
to obtain a RandomGenerator
object that is shared by all classes in the application.public RandomGenerator(long seed)
public <T> T nextChoice(T... choices)
choices
- a set of choicespublic <T> T nextChoice(List<T> choices)
choices
- a set of choicespublic <T> T nextChoice(Iterable<T> choices)
choices
- a set of choicespublic int nextInt()
public int nextInt(int n)
n
-1,
inclusive. This method is in modern implementations of the
Random
class, but is missing from JDK 1.1.public 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 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
, inclusivepublic 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 < high
public 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 p
public Color nextColor()
Color
public static RandomGenerator getInstance()
RandomGenerator
instance that can be shared among
several classes.RandomGenerator
objectpublic static void clearFixedIntegers()
public static void feedFixedIntegers(int... numbers)
numbers
- public static void feedFixedIntegers(Iterable<Integer> numbers)
numbers
- public static void setSharedSeed(long seed)
rg.setSeed(seed);
on that object.seed
-