RandomGenerator
interface is designed to provide a common
protocol for objects that generate random or (more typically) pseudorandom
sequences of numbers (or Boolean values). Such a sequence may be obtained by
either repeatedly invoking a method that returns a single pseudorandomly
chosen value, or by invoking a method that returns a stream of
pseudorandomly chosen values.
Ideally, given an implicitly or explicitly specified range of values, each value would be chosen independently and uniformly from that range. In practice, one may have to settle for some approximation to independence and uniformity.
In the case of int
, long
, and boolean
values, if
there is no explicit specification of range, then the range includes all
possible values of the type. In the case of float
and double
values, first a value is always chosen uniformly from the set of
2w values between 0.0 (inclusive) and 1.0 (exclusive),
where w is Float#PRECISION
for float
values
and Double#PRECISION
for double
values, such that adjacent values differ by 2−w
(notice that this set is a subset of the set of
all representable floating-point values between 0.0 (inclusive) and 1.0 (exclusive));
then if an explicit range was specified, then the chosen number is
computationally scaled and translated so as to appear to have been chosen
approximately uniformly from that explicit range.
Each method that returns a stream produces a stream of values each of
which is chosen in the same manner as for a method that returns a single
pseudorandomly chosen value. For example, if r
implements
RandomGenerator
, then the method call r.ints(100)
returns a
stream of 100 int
values. These are not necessarily the exact same
values that would have been returned if instead r.nextInt()
had been
called 100 times; all that is guaranteed is that each value in the stream is
chosen in a similar pseudorandom manner from the same range.
Every object that implements the RandomGenerator
interface by
using a pseudorandom algorithm is assumed to contain a finite amount of
state. Using such an object to generate a pseudorandomly chosen value alters
its state by computing a new state as a function of the current state,
without reference to any information other than the current state. The number
of distinct possible states of such an object is called its period.
(Some implementations of the RandomGenerator
interface may be truly
random rather than pseudorandom, for example relying on the statistical
behavior of a physical object to derive chosen values. Such implementations
do not have a fixed period.)
As a rule, objects that implement the RandomGenerator
interface
need not be thread-safe. It is recommended that multithreaded applications
use either ThreadLocalRandom
or (preferably) pseudorandom number
generators that implement the SplittableGenerator
or
JumpableGenerator
interface.
Objects that implement RandomGenerator
are typically not
cryptographically secure. Consider instead using SecureRandom
to get
a cryptographically secure pseudorandom number generator for use by
security-sensitive applications. Note, however, that SecureRandom
does implement the RandomGenerator
interface, so that instances of
SecureRandom
may be used interchangeably with other types of
pseudorandom generators in applications that do not require a secure
generator.
Unless explicit stated otherwise, the use of null for any method argument will cause a NullPointerException.
Modifier and Type | Class and Description |
---|---|
public static interface | RandomGenerator.
This interface is designed to provide a common protocol for objects that generate sequences of pseudorandom values and can easily jump forward, by an arbitrary amount, to a distant point in the state cycle. |
public static interface | RandomGenerator.
This interface is designed to provide a common protocol for objects that generate pseudorandom values and can easily jump forward, by a moderate amount (ex. |
public static interface | RandomGenerator.
This interface is designed to provide a common protocol for objects that generate sequences of pseudorandom values and can easily not only jump but also leap forward, by a large amount (ex. |
public static interface | RandomGenerator.
This interface is designed to provide a common protocol for objects that generate sequences of pseudorandom values and can be split into two objects (the original one and a new one) each of which obey that same protocol (and therefore can be recursively split indefinitely). |
public static interface | RandomGenerator.
The |
Modifier and Type | Method and Description |
---|---|
public default DoubleStream | Returns: a stream of pseudorandomly chosendouble valuesReturns an effectively unlimited stream of pseudorandomly chosen
|
public default DoubleStream | Returns: a stream of pseudorandomly chosendouble values, each between
the specified origin (inclusive) and the specified bound (exclusive)the least value that can be produced randomNumberOrigin, double the upper bound (exclusive) for each value produced randomNumberBound)Returns an effectively unlimited stream of pseudorandomly chosen
|
public default DoubleStream | Returns: a stream of pseudorandomly chosendouble valuesthe number of values to generate streamSize)Returns a stream producing the given |
public default DoubleStream | Returns: a stream of pseudorandomly chosendouble values, each between
the specified origin (inclusive) and the specified bound (exclusive)the number of values to generate streamSize, double the least value that can be produced randomNumberOrigin, double the upper bound (exclusive) for each value produced randomNumberBound)Returns a stream producing the given |
public default DoubleStream | Returns: a stream of pseudorandomly chosendouble values, each
between left and right , as specified above.the left boundary left, double the right boundary right, boolean whether the isLeftIncluded, boolean left boundary is includedwhether the isRightIncluded)right boundary is includedReturns an effectively unlimited stream of pseudorandomly chosen
|
public static RandomGenerator | Returns: aRandomGenerator Returns a |
public default IntStream | Returns: a stream of pseudorandomly chosenint valuesReturns an effectively unlimited stream of pseudorandomly chosen
|
public default IntStream | Returns: a stream of pseudorandomly chosenint values, each between
the specified origin (inclusive) and the specified bound (exclusive)the least value that can be produced randomNumberOrigin, int the upper bound (exclusive) for each value produced randomNumberBound)Returns an effectively unlimited stream of pseudorandomly chosen
|
public default IntStream | Returns: a stream of pseudorandomly chosenint valuesthe number of values to generate streamSize)Returns a stream producing the given |
public default IntStream | Returns: a stream of pseudorandomly chosenint values, each between
the specified origin (inclusive) and the specified bound (exclusive)the number of values to generate streamSize, int the least value that can be produced randomNumberOrigin, int the upper bound (exclusive) for each value produced randomNumberBound)Returns a stream producing the given |
public default boolean | Returns: true if the implementation of RandomGenerator (algorithm) has been marked for deprecationReturn true if the implementation of RandomGenerator (algorithm) has been marked for deprecation. |
public default LongStream | Returns: a stream of pseudorandomly chosenlong valuesReturns an effectively unlimited stream of pseudorandomly chosen
|
public default LongStream | Returns: a stream of pseudorandomly chosenlong values, each between
the specified origin (inclusive) and the specified bound (exclusive)the least value that can be produced randomNumberOrigin, long the upper bound (exclusive) for each value produced randomNumberBound)Returns an effectively unlimited stream of pseudorandomly chosen
|
public default LongStream | Returns: a stream of pseudorandomly chosenlong valuesthe number of values to generate streamSize)Returns a stream producing the given |
public default LongStream | Returns: a stream of pseudorandomly chosenlong values, each between
the specified origin (inclusive) and the specified bound (exclusive)the number of values to generate streamSize, long the least value that can be produced randomNumberOrigin, long the upper bound (exclusive) for each value produced randomNumberBound)Returns a stream producing the given |
public default boolean | Returns: a pseudorandomly chosenboolean valueReturns a pseudorandomly chosen |
public default void | nextBytes(byte[]
the byte array to fill with pseudorandom bytes bytes)Fills a user-supplied byte array with generated byte values pseudorandomly chosen uniformly from the range of values between -128 (inclusive) and 127 (inclusive). |
public default double | Returns: a pseudorandomdouble value between zero (inclusive)
and one (exclusive)Returns a pseudorandom |
public default double | Returns: a pseudorandomly chosendouble value between
zero (inclusive) and the bound (exclusive)the upper bound (exclusive) for the returned value.
Must be positive and finite bound)Returns a pseudorandomly chosen |
public default double | Returns: a pseudorandomly chosendouble value between the
origin (inclusive) and the bound (exclusive)the least value that can be returned origin, double the upper bound (exclusive) for the returned value bound)Returns a pseudorandomly chosen |
public default double | Returns: a nonnegativedouble value pseudorandomly chosen from an
exponential distributionReturns a nonnegative |
public default float | Returns: a pseudorandomfloat value between zero (inclusive) and one (exclusive)Returns a pseudorandom |
public default float | Returns: a pseudorandomly chosenfloat value between
zero (inclusive) and the bound (exclusive)the upper bound (exclusive) for the returned value.
Must be positive and finite bound)Returns a pseudorandomly chosen |
public default float | Returns: a pseudorandomly chosenfloat value between the
origin (inclusive) and the bound (exclusive)the least value that can be returned origin, float the upper bound (exclusive) bound)Returns a pseudorandomly chosen |
public default double | Returns: adouble value pseudorandomly chosen from a
Gaussian distributionReturns a |
public default double | Returns: adouble value pseudorandomly chosen from the
specified Gaussian distributionthe mean of the Gaussian distribution to be drawn from mean, double the standard deviation (square root of the variance)
of the Gaussian distribution to be drawn from stddev)Returns a |
public default int | |
public default int | Returns: a pseudorandomly chosenint value between
zero (inclusive) and the bound (exclusive)the upper bound (exclusive) for the returned value.
Must be positive. bound)Returns a pseudorandomly chosen |
public default int | Returns: a pseudorandomly chosenint value between the
origin (inclusive) and the bound (exclusive)the least value that can be returned origin, int the upper bound (exclusive) for the returned value bound)Returns a pseudorandomly chosen |
public long | |
public default long | Returns: a pseudorandomly chosenlong value between
zero (inclusive) and the bound (exclusive)the upper bound (exclusive) for the returned value.
Must be positive. bound)Returns a pseudorandomly chosen |
public default long | Returns: a pseudorandomly chosenlong value between the
origin (inclusive) and the bound (exclusive)the least value that can be returned origin, long the upper bound (exclusive) for the returned value bound)Returns a pseudorandomly chosen |
public static RandomGenerator | Returns: An instance ofRandomGenerator Name of random number generator
algorithm name)Returns an instance of |