Random

public class Random extends Object
implements Serializable
Known Direct Subclasses

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method random() simpler to use.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.

Public Constructor Summary

Random()
Creates a new random number generator.
Random(long seed)
Creates a new random number generator using a single long seed.

Public Method Summary

DoubleStream
doubles(long streamSize)
Returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).
DoubleStream
doubles()
Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).
DoubleStream
doubles(double randomNumberOrigin, double randomNumberBound)
Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
DoubleStream
doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
Returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
IntStream
ints(long streamSize)
Returns a stream producing the given streamSize number of pseudorandom int values.
IntStream
ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
IntStream
ints(int randomNumberOrigin, int randomNumberBound)
Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
IntStream
ints()
Returns an effectively unlimited stream of pseudorandom int values.
LongStream
longs()
Returns an effectively unlimited stream of pseudorandom long values.
LongStream
longs(long streamSize)
Returns a stream producing the given streamSize number of pseudorandom long values.
LongStream
longs(long randomNumberOrigin, long randomNumberBound)
Returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).
LongStream
longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
Returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).
boolean
nextBoolean()
Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
void
nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array.
double
nextDouble()
Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
float
nextFloat()
Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
synchronized double
nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.
int
nextInt()
Returns the next pseudorandom, uniformly distributed int value 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.
long
nextLong()
Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
synchronized void
setSeed(long seed)
Sets the seed of this random number generator using a single long seed.

Protected Method Summary

int
next(int bits)
Generates the next pseudorandom number.

Inherited Method Summary

Public Constructors

public