Top Description Inners Methods
java.util.random

public Interface RandomGenerator

Known Direct Subinterfaces
java.util.random.RandomGenerator.StreamableGenerator
Known Direct Implementers
jdk.internal.util.random.RandomSupport.AbstractSpliteratorGenerator, java.util.Random, java.util.Random.RandomWrapper, java.util.SplittableRandom
Imports
java.math.BigInteger, java.security.SecureRandom, java.util.Objects, java.util.concurrent.ThreadLocalRandom, jdk.internal.util.random.RandomSupport, java.util.stream.DoubleStream, .IntStream, .LongStream, .Stream

The 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 2w (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.

Since
17

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
RandomGenerator.ArbitrarilyJumpableGenerator

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.JumpableGenerator

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.LeapableGenerator

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.SplittableGenerator

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.StreamableGenerator

The StreamableGenerator interface augments the RandomGenerator interface to provide methods that return streams of RandomGenerator objects.

Method Summary

Modifier and TypeMethod and Description
public default DoubleStream

Returns:

a stream of pseudorandomly chosen double values
doubles
()

Returns an effectively unlimited stream of pseudorandomly chosen double values.

public default DoubleStream

Returns:

a stream of pseudorandomly chosen double values, each between the specified origin (inclusive) and the specified bound (exclusive)
doubles
(double
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 double values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

public default DoubleStream

Returns:

a stream of pseudorandomly chosen double values
doubles
(long
the number of values to generate
streamSize
)

Returns a stream producing the given streamSize number of pseudorandomly chosen double values.

public default DoubleStream

Returns:

a stream of pseudorandomly chosen double values, each between the specified origin (inclusive) and the specified bound (exclusive)
doubles
(long
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 streamSize number of pseudorandomly chosen double values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

public default DoubleStream

Returns:

a stream of pseudorandomly chosen double values, each between left and right, as specified above.
equiDoubles
(double
the left boundary
left
,
double
the right boundary
right
,
boolean
whether the left boundary is included
isLeftIncluded
,
boolean
whether the right boundary is included
isRightIncluded
)

Returns an effectively unlimited stream of pseudorandomly chosen double values, where each value is between the specified left boundary and the specified right boundary.

public static RandomGenerator

Returns:

a RandomGenerator
getDefault
()

Returns a RandomGenerator meeting the minimal requirement of having an algorithm whose state bits are greater than or equal 64.

public default IntStream

Returns:

a stream of pseudorandomly chosen int values
ints
()

Returns an effectively unlimited stream of pseudorandomly chosen int values.

public default IntStream

Returns:

a stream of pseudorandomly chosen int values, each between the specified origin (inclusive) and the specified bound (exclusive)
ints
(int
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 int values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

public default IntStream

Returns:

a stream of pseudorandomly chosen int values
ints
(long
the number of values to generate
streamSize
)

Returns a stream producing the given streamSize number of pseudorandomly chosen int values.

public default IntStream

Returns:

a stream of pseudorandomly chosen int values, each between the specified origin (inclusive) and the specified bound (exclusive)
ints
(long
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 streamSize number of pseudorandomly chosen int values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

public default boolean

Returns:

true if the implementation of RandomGenerator (algorithm) has been marked for deprecation
isDeprecated
()

Return true if the implementation of RandomGenerator (algorithm) has been marked for deprecation.

public default LongStream

Returns:

a stream of pseudorandomly chosen long values
longs
()

Returns an effectively unlimited stream of pseudorandomly chosen long values.

public default LongStream

Returns:

a stream of pseudorandomly chosen long values, each between the specified origin (inclusive) and the specified bound (exclusive)
longs
(long
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 long values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

public default LongStream

Returns:

a stream of pseudorandomly chosen long values
longs
(long
the number of values to generate
streamSize
)

Returns a stream producing the given streamSize number of pseudorandomly chosen long values.

public default LongStream

Returns:

a stream of pseudorandomly chosen long values, each between the specified origin (inclusive) and the specified bound (exclusive)
longs
(long
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 streamSize number of pseudorandomly chosen long values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

public default boolean

Returns:

a pseudorandomly chosen boolean value
nextBoolean
()

Returns a pseudorandomly chosen boolean value.

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 pseudorandom double value between zero (inclusive) and one (exclusive)
nextDouble
()

Returns a pseudorandom double value between zero (inclusive) and one (exclusive).

public default double

Returns:

a pseudorandomly chosen double value between zero (inclusive) and the bound (exclusive)
nextDouble
(double
the upper bound (exclusive) for the returned value. Must be positive and finite
bound
)

Returns a pseudorandomly chosen double value between zero (inclusive) and the specified bound (exclusive).

public default double

Returns:

a pseudorandomly chosen double value between the origin (inclusive) and the bound (exclusive)
nextDouble
(double
the least value that can be returned
origin
,
double
the upper bound (exclusive) for the returned value
bound
)

Returns a pseudorandomly chosen double value between the specified origin (inclusive) and the specified bound (exclusive).

public default double

Returns:

a nonnegative double value pseudorandomly chosen from an exponential distribution
nextExponential
()

Returns a nonnegative double value pseudorandomly chosen from an exponential distribution whose mean is 1.

public default float

Returns:

a pseudorandom float value between zero (inclusive) and one (exclusive)
nextFloat
()

Returns a pseudorandom float value between zero (inclusive) and one (exclusive).

public default float

Returns:

a pseudorandomly chosen float value between zero (inclusive) and the bound (exclusive)
nextFloat
(float
the upper bound (exclusive) for the returned value. Must be positive and finite
bound
)

Returns a pseudorandomly chosen float value between zero (inclusive) and the specified bound (exclusive).

public default float

Returns:

a pseudorandomly chosen float value between the origin (inclusive) and the bound (exclusive)
nextFloat
(float
the least value that can be returned
origin
,
float
the upper bound (exclusive)
bound
)

Returns a pseudorandomly chosen float value between the specified origin (inclusive) and the specified bound (exclusive).

public default double

Returns:

a double value pseudorandomly chosen from a Gaussian distribution
nextGaussian
()

Returns a double value pseudorandomly chosen from a Gaussian (normal) distribution whose mean is 0 and whose standard deviation is 1.

public default double

Returns:

a double value pseudorandomly chosen from the specified Gaussian distribution
nextGaussian
(double
the 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 double value pseudorandomly chosen from a Gaussian (normal) distribution with a mean and standard deviation specified by the arguments.

public default int

Returns:

a pseudorandomly chosen int value
nextInt
()

Returns a pseudorandomly chosen int value.

public default int

Returns:

a pseudorandomly chosen int value between zero (inclusive) and the bound (exclusive)
nextInt
(int
the upper bound (exclusive) for the returned value. Must be positive.
bound
)

Returns a pseudorandomly chosen int value between zero (inclusive) and the specified bound (exclusive).

public default int

Returns:

a pseudorandomly chosen int value between the origin (inclusive) and the bound (exclusive)
nextInt
(int
the least value that can be returned
origin
,
int
the upper bound (exclusive) for the returned value
bound
)

Returns a pseudorandomly chosen int value between the specified origin (inclusive) and the specified bound (exclusive).

public long

Returns:

a pseudorandomly chosen long value
nextLong
()

Returns a pseudorandomly chosen long value.

public default long

Returns:

a pseudorandomly chosen long value between zero (inclusive) and the bound (exclusive)
nextLong
(long
the upper bound (exclusive) for the returned value. Must be positive.
bound
)

Returns a pseudorandomly chosen long value between zero (inclusive) and the specified bound (exclusive).

public default long

Returns:

a pseudorandomly chosen long value between the origin (inclusive) and the bound (exclusive)
nextLong
(long
the least value that can be returned
origin
,
long
the upper bound (exclusive) for the returned value
bound
)

Returns a pseudorandomly chosen long value between the specified origin (inclusive) and the specified bound (exclusive).

public static RandomGenerator

Returns:

An instance of RandomGenerator
of
(String
Name of random number generator algorithm
name
)

Returns an instance of RandomGenerator that utilizes the name algorithm.