java.util.random
This package contains classes and interfaces that support a generic API for random number generation.These classes and interfaces support the definition and use of "random generators", a term covering what have traditionally been called "random number generators" as well as generators of other sorts of randomly chosen values (eg. booleans). These classes and interfaces cover not only deterministic (pseudorandom) algorithms but also generators of values that use some "truly random" physical source (stochastic algorithms perhaps making use of thermal noise, for example, or quantum-mechanical effects).
The principal interface is RandomGenerator, which provides
methods for requesting individual values of type int, long,
float, double, or boolean chosen pseudorandomly
from a uniform distribution; methods for requesting values of type
double chosen pseudorandomly from a normal distribution or from an
exponential distribution; and methods for creating streams of values of type
int, long, or double chosen pseudorandomly from a
uniform distribution (such streams are spliterator-based, allowing for
parallel processing of their elements). There are also static factory methods
for creating an instance of a specific random number generator algorithm
given its name.
The principal supporting class is RandomGeneratorFactory. This
can be used to generate multiple random number generators for a specific
algorithm. RandomGeneratorFactory also provides methods for
selecting random number generator algorithms.
An important subsidiary interface is
RandomGenerator.StreamableGenerator, which provides methods for
creating spliterator-based streams of RandomGenerator objects,
allowing for parallel processing of these objects using multiple threads.
Unlike Random, most implementations of
RandomGenerator are not thread-safe. The intent is that
instances should not be shared among threads; rather, each thread should have
its own random generator(s) to use. The various pseudorandom algorithms
provided by this package are designed so that multiple instances will (with
very high probability) behave as if statistically independent.
For many purposes, these are the only two interfaces that a consumer of
pseudorandom values will need. There are also some more specialized
interfaces that describe more specialized categories of random number
generators SplittableGenerator,
JumpableGenerator,
LeapableGenerator, and
ArbitrarilyJumpableGenerator
that have specific strategies for creating statistically independent instances.
Using the Random Number Generator Interfaces
To get started, an application should first create one instance of a generator class. Assume that the contents of the packagejava.util.random has been imported:
import java.util.random.*;
Then one can choose a specific implementation by giving the name of a generator
algorithm to the static method RandomGenerator.of, in which case the
no-arguments constructor for that implementation is used:
RandomGenerator g = RandomGenerator.of("L64X128MixRandom");
For a single-threaded application, this is all that is needed. One can then
invoke methods of g such as