Java Reference

In-Depth Information

Math.random()
generates a different sequence each time an application starts run-

ning.

Tip
Random(long seed)
gives you the opportunity to reuse the same seed

value,allowingthesamesequenceofrandomnumberstobegenerated.Youwillfind

thiscapabilityusefulwhendebuggingafaultyapplicationthatinvolvesrandomnum-

bers.

Random(long seed)
callsthe
void setSeed(long seed)
methodtoset

theseedtothespecifiedvalue.Ifyoucall
setSeed()
afterinstantiating
Random
,the

random number generator is reset to the state that it was in immediately after calling

Random(long seed)
.

The previous code fragment demonstrates
Random
's
double nextDouble()

method, which returns the next pseudorandom, uniformly distributed double precision

floating-point value between 0.0 and 1.0 in this random number generator's sequence.

Random
also declares the following methods for returning other kinds of values:

•
boolean nextBoolean()
returnsthenextpseudorandom,uniformlydis-

tributedBooleanvalueinthisrandomnumbergenerator'ssequence.Valuestrue

and false are generated with (approximately) equal probability.

•
void nextBytes(byte[] bytes)
generatespseudorandombyteinteger

values and stores them in the
bytes
array. The number of generated bytes is

equal to the length of the
bytes
array.

•
float nextFloat()
returnsthenextpseudorandom,uniformlydistributed

floating-pointvaluebetween0.0and1.0inthisrandomnumbergenerator'sse-

quence.

•
double nextGaussian()
returnsthenextpseudorandom,Gaussian(“nor-

mally”) distributed double precision floating-point value with mean 0.0 and

standard deviation 1.0 in this random number generator's sequence.

•
int nextInt()
returns the next pseudorandom, uniformly distributed in-

tegervalueinthisrandomnumbergenerator'ssequence.All2
32
possibleinteger

values are generated with (approximately) equal probability.

•
int nextInt(int n)
returns a pseudorandom, uniformly distributed in-

teger value between 0 (inclusive) and the specified value (exclusive), drawn

from this random number generator's sequence. All
n
possible integer values

are generated with (approximately) equal probability.