public class RandNumGenerator
extends java.util.Random
RandNumGenerator
class provides a singleton
object for random number generation. RandNumGenerator
extends the java.util.Random
class, so the object of this
class inherits all methods defined for the Random
class.
Using this class, though, many different objects can share a single
source of random numbers. This eliminates the potential problem of
having multiple random number generators generating sequences of
numbers that are too similar.
Example of how to use RandNumGenerator
:
RandNumGenerator randNumGen = RandNumGenerator.getInstance();
int anInt = randNumGen.nextInt(4);
double aDouble = randNumGen.nextDouble();
The original RandNumGenerator
class, which provided
a getInstance method but did not extend Random
, was
copyright© 2002 College Entrance Examination Board
(www.collegeboard.com).
Modifier and Type | Method and Description |
---|---|
static RandNumGenerator |
getInstance()
Returns a random number generator.
|
boolean |
nextBoolean()
Returns a pseudorandom, uniformly distributed
boolean
value from this random number generator's sequence. |
double |
nextDouble()
Returns a pseudorandom, uniformly distributed
double
value between 0.0 and 1.0 from this
random number generator's sequence. |
int |
nextInt(int n)
Returns a pseudorandom, uniformly distributed
int value
between 0 (inclusive) and the specified value (exclusive),
drawn from this random number generator's sequence. |
void |
setSeed(long seed)
Sets the seed of this random number generator using a single
long seed. |
public static RandNumGenerator getInstance()
RandNumGenerator
object to
provide a better sequence of random numbers.public void setSeed(long seed)
long
seed. For more technical details, see the
class documentation for the Random
class.setSeed
in class java.util.Random
Random.setSeed(long)
public boolean nextBoolean()
boolean
value from this random number generator's sequence. The values
true
and false
are produced with
(approximately) equal probability. For more technical details,
see the class documentation for the Random
class.nextBoolean
in class java.util.Random
boolean
value from this random number generator's sequenceRandom.nextBoolean()
public double nextDouble()
double
value between 0.0
and 1.0
from this
random number generator's sequence. For more technical details,
see the class documentation for the Random
class.nextDouble
in class java.util.Random
double
value between 0.0
and 1.0
from
this random number generator's sequence.Random.nextDouble()
public int nextInt(int n)
int
value
between 0
(inclusive) and the specified value (exclusive),
drawn from this random number generator's sequence. For more
technical details, see the class documentation for the
Random
class.nextInt
in class java.util.Random
n
- the bound on the random number to be returned.
Must be positive.int
value between 0 (inclusive) and n
(exclusive).java.lang.IllegalArgumentException
- n
is not positiveRandom.nextInt(int)