Java Reference
In-Depth Information
22.4.
Random
The
Random
class creates objects that manage independent sequences of
pseudorandom numbers. If you don't care what the sequence is and want
it as a sequence of
double
values, use the method
java.lang.Math.random
,
which creates a single
Random
object the first time it is invoked and returns
pseudorandom numbers from that objectsee
Section 22.9
on page
657
.
You can gain more control over the sequence (for example, the ability to
set the seed) by creating a
Random
object and getting values from it.
public
Random()
Creates a new random number generator. Its seed will be ini-
tialized to a value based on the current time.
public
Random(long seed)
Creates a new random number generator using the specified
seed
. Two
Random
objects created with the same initial
seed
will
return the same sequence of pseudorandom numbers.
public void
setSeed(long seed)
Sets the seed of the random number generator to
seed
. This
method can be invoked at any time and resets the sequence to
start with the given seed.
public boolean
nextBoolean()
Returns a pseudorandom uniformly distributed
boolean
value.
public int
nextInt()
Returns
a
pseudorandom
uniformly
distributed
int
value
between
the
two
values
and
Integer.MIN_VALUE
In-
teger.MAX_VALUE
, inclusive.