Class SplittableRandom
- All Implemented Interfaces:
RandomGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator
public final class SplittableRandom extends Object implements RandomGenerator, RandomGenerator.SplittableGenerator
SplittableRandom supports methods for producing pseudorandom numbers of type int, long, and double with similar usages as for class Random but differs in the following ways: - Series of generated values pass the DieHarder suite testing independence and uniformity properties of random number generators. (Most recently validated with version 3.31.1.) These tests validate only the methods for certain types and ranges, but similar properties are expected to hold, at least approximately, for others as well. The period (length of any series of generated values before it repeats) is 264.
- Method
split()constructs and returns a new SplittableRandom instance that shares no mutable state with the current instance. However, with very high probability, the values collectively generated by the two objects have the same statistical properties as if the same quantity of values were generated by a single thread using a singleSplittableRandomobject. - Instances of SplittableRandom are not thread-safe. They are designed to be split, not shared, across threads. For example, a
fork/join-stylecomputation using random numbers might include a construction of the formnew Subtask(aSplittableRandom.split()).fork(). - This class provides additional methods for generating random streams, that employ the above techniques when used in
stream.parallel()mode.
Instances of SplittableRandom are not cryptographically secure. Consider instead using SecureRandom in security-sensitive applications. Additionally, default-constructed instances do not use a cryptographically random seed unless the system property java.util.secureRandomSeed is set to true.
- Since:
- 1.8
Nested Class Summary
Nested classes/interfaces declared in interface RandomGenerator
RandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator | Modifier and Type | Interface | Description |
|---|---|---|
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. |
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. 264) to a distant point in the state cycle. |
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. 2128), to a very distant point in the state cycle. |
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). |
static interface |
RandomGenerator.StreamableGenerator |
The RandomGenerator.StreamableGenerator interface augments the RandomGenerator interface to provide methods that return streams of RandomGenerator objects. |
Constructor Summary
| Constructor | Description |
|---|---|
SplittableRandom() |
Creates a new SplittableRandom instance that is likely to generate sequences of values that are statistically independent of those of any other instances in the current program; and may, and typically does, vary across program invocations. |
SplittableRandom |
Creates a new SplittableRandom instance using the specified initial seed. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
DoubleStream |
doubles() |
Returns an effectively unlimited stream of pseudorandom
double values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive). |
DoubleStream |
doubles |
Returns an effectively unlimited stream of pseudorandom
double values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive). |
DoubleStream |
doubles |
Returns a stream producing the given streamSize number of pseudorandom double values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive). |
DoubleStream |
doubles |
Returns a stream producing the given streamSize number of pseudorandom double values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive). |
IntStream |
ints() |
Returns an effectively unlimited stream of pseudorandom int values from this generator and/or one split from it. |
IntStream |
ints |
Returns an effectively unlimited stream of pseudorandom
int values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive). |
IntStream |
ints |
Returns a stream producing the given streamSize number of pseudorandom int values from this generator and/or one split from it. |
IntStream |
ints |
Returns a stream producing the given streamSize number of pseudorandom int values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive). |
LongStream |
longs() |
Returns an effectively unlimited stream of pseudorandom
long values from this generator and/or one split from it. |
LongStream |
longs |
Returns a stream producing the given streamSize number of pseudorandom long values from this generator and/or one split from it. |
LongStream |
longs |
Returns an effectively unlimited stream of pseudorandom
long values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive). |
LongStream |
longs |
Returns a stream producing the given streamSize number of pseudorandom long values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive). |
void |
nextBytes |
Fills a user-supplied byte array with generated byte values pseudorandomly chosen uniformly from the range of values between -128 (inclusive) and 127 (inclusive). |
long |
nextLong() |
Returns a pseudorandomly chosen long value. |
SplittableRandom |
split() |
Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance. |
SplittableRandom |
split |
Returns a new pseudorandom number generator, split off from this one, that implements the RandomGenerator and RandomGenerator.SplittableGenerator interfaces. |
Stream |
splits() |
Returns an effectively unlimited stream of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface. |
Stream |
splits |
Returns a stream producing the given streamSize number of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface. |
Stream |
splits |
Returns a stream producing the given streamSize number of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface. |
Stream |
splits |
Returns an effectively unlimited stream of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface. |
Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | Modifier and Type | Method | Description |
|---|---|---|
protected Object |
clone() |
Creates and returns a copy of this object. |
boolean |
equals |
Indicates whether some other object is "equal to" this one. |
protected void |
finalize() |
Deprecated, for removal: This API element is subject to removal in a future version. Finalization is deprecated and subject to removal in a future release. |
final Class |
getClass() |
Returns the runtime class of this Object. |
int |
hashCode() |
Returns a hash code value for this object. |
final void |
notify() |
Wakes up a single thread that is waiting on this object's monitor. |
final void |
notifyAll() |
Wakes up all threads that are waiting on this object's monitor. |
String |
toString() |
Returns a string representation of the object. |
final void |
wait() |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
Methods declared in interface RandomGenerator
equiDoubles, isDeprecated, nextBoolean, nextDouble, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextFloat, nextGaussian, nextGaussian, nextInt, nextInt, nextInt, nextLong, nextLong | Modifier and Type | Method | Description |
|---|---|---|
default DoubleStream |
equiDoubles |
Returns an effectively unlimited stream of pseudorandomly chosen double values, where each value is between the specified left boundary and the specified right boundary. |
default boolean |
isDeprecated() |
Return true if the implementation of RandomGenerator (algorithm) has been marked for deprecation. |
default boolean |
nextBoolean() |
Returns a pseudorandomly chosen boolean value. |
default double |
nextDouble() |
Returns a pseudorandom double value between zero (inclusive) and one (exclusive). |
default double |
nextDouble |
Returns a pseudorandomly chosen double value between zero (inclusive) and the specified bound (exclusive). |
default double |
nextDouble |
Returns a pseudorandomly chosen double value between the specified origin (inclusive) and the specified bound (exclusive). |
default double |
nextExponential() |
Returns a nonnegative double value pseudorandomly chosen from an exponential distribution whose mean is 1. |
default float |
nextFloat() |
Returns a pseudorandom float value between zero (inclusive) and one (exclusive). |
default float |
nextFloat |
Returns a pseudorandomly chosen float value between zero (inclusive) and the specified bound (exclusive). |
default float |
nextFloat |
Returns a pseudorandomly chosen float value between the specified origin (inclusive) and the specified bound (exclusive). |
default double |
nextGaussian() |
Returns a double value pseudorandomly chosen from a Gaussian (normal) distribution whose mean is 0 and whose standard deviation is 1. |
default double |
nextGaussian |
Returns a double value pseudorandomly chosen from a Gaussian (normal) distribution with a mean and standard deviation specified by the arguments. |
default int |
nextInt() |
Returns a pseudorandomly chosen int value. |
default int |
nextInt |
Returns a pseudorandomly chosen int value between zero (inclusive) and the specified bound (exclusive). |
default int |
nextInt |
Returns a pseudorandomly chosen int value between the specified origin (inclusive) and the specified bound (exclusive). |
default long |
nextLong |
Returns a pseudorandomly chosen long value between zero (inclusive) and the specified bound (exclusive). |
default long |
nextLong |
Returns a pseudorandomly chosen long value between the specified origin (inclusive) and the specified bound (exclusive). |
Methods declared in interface RandomGenerator.SplittableGenerator
rngs, rngs | Modifier and Type | Method | Description |
|---|---|---|
default Stream |
rngs() |
Returns an effectively unlimited stream of new pseudorandom number generators, each of which implements the RandomGenerator interface. |
default Stream |
rngs |
Returns a stream producing the given streamSize number of new pseudorandom number generators, each of which implements the RandomGenerator interface. |
Constructor Details
SplittableRandom
public SplittableRandom(long seed)
- Parameters:
-
seed- the initial seed
SplittableRandom
public SplittableRandom()
Method Details
split
public SplittableRandom split()
split() method, and the same expected statistical properties apply to the entire set of generators constructed by such recursive splitting.- Specified by:
-
splitin interfaceRandomGenerator.SplittableGenerator - Returns:
- the new SplittableRandom instance
split
public SplittableRandom split(RandomGenerator.SplittableGenerator source)
RandomGenerator and RandomGenerator.SplittableGenerator interfaces.- Specified by:
-
splitin interfaceRandomGenerator.SplittableGenerator - Parameters:
-
source- aRandomGenerator.SplittableGeneratorinstance to be used instead of this one as a source of pseudorandom bits used to initialize the state of the new ones. - Returns:
- an object that implements the
RandomGeneratorandRandomGenerator.SplittableGeneratorinterfaces - Throws:
-
NullPointerException- if source is null - Since:
- 17
nextLong
public long nextLong()
RandomGeneratorlong value.- Specified by:
-
nextLongin interfaceRandomGenerator - Returns:
- a pseudorandomly chosen
longvalue
nextBytes
public void nextBytes(byte[] bytes)
- Specified by:
-
nextBytesin interfaceRandomGenerator - Parameters:
-
bytes- the byte array to fill with pseudorandom bytes - Throws:
-
NullPointerException- if bytes is null - Since:
- 10
splits
public Stream<RandomGenerator.SplittableGenerator> splits()
RandomGenerator.SplittableGenerator interface. This pseudorandom number generator may be used as a source of pseudorandom bits used to initialize the state the new ones.
- Specified by:
-
splitsin interfaceRandomGenerator.SplittableGenerator - Implementation Requirements:
- The default implementation invokes
splits(this). - Returns:
- a stream of
RandomGenerator.SplittableGeneratorobjects - Since:
- 17
splits
public Stream<RandomGenerator.SplittableGenerator> splits(long streamSize)
streamSize number of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface. This pseudorandom number generator may be used as a source of pseudorandom bits used to initialize the state the new ones.
- Specified by:
-
splitsin interfaceRandomGenerator.SplittableGenerator - Implementation Requirements:
- Parameters:
-
streamSize- the number of values to generate - Returns:
- a stream of
RandomGenerator.SplittableGeneratorobjects - Throws:
-
IllegalArgumentException- ifstreamSizeis less than zero - Since:
- 17
splits
public Stream<RandomGenerator.SplittableGenerator> splits(RandomGenerator.SplittableGenerator source)
RandomGenerator.SplittableGenerator interface.- Specified by:
-
splitsin interfaceRandomGenerator.SplittableGenerator - Implementation Requirements:
- Parameters:
-
source- aRandomGenerator.SplittableGeneratorinstance to be used instead of this one as a source of pseudorandom bits used to initialize the state of the new ones. - Returns:
- a stream of
RandomGenerator.SplittableGeneratorobjects - Throws:
-
NullPointerException- if source is null - Since:
- 17
splits
public Stream<RandomGenerator.SplittableGenerator> splits(long streamSize, RandomGenerator.SplittableGenerator source)
streamSize number of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface.- Specified by:
-
splitsin interfaceRandomGenerator.SplittableGenerator - Implementation Requirements:
- Parameters:
-
streamSize- the number of values to generate -
source- aRandomGenerator.SplittableGeneratorinstance to be used instead of this one as a source of pseudorandom bits used to initialize the state of the new ones. - Returns:
- a stream of
RandomGenerator.SplittableGeneratorobjects - Throws:
-
NullPointerException- if source is null -
IllegalArgumentException- ifstreamSizeis less than zero - Since:
- 17
ints
public IntStream ints(long streamSize)
streamSize number of pseudorandom int values from this generator and/or one split from it.- Specified by:
-
intsin interfaceRandomGenerator - Parameters:
-
streamSize- the number of values to generate - Returns:
- a stream of pseudorandom
intvalues - Throws:
-
IllegalArgumentException- ifstreamSizeis less than zero
ints
public IntStream ints()
int values from this generator and/or one split from it.- Specified by:
-
intsin interfaceRandomGenerator - Implementation Note:
- This method is implemented to be equivalent to
ints(Long.MAX_VALUE). - Returns:
- a stream of pseudorandom
intvalues
ints
public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
streamSize number of pseudorandom int values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
-
intsin interfaceRandomGenerator - Parameters:
-
streamSize- the number of values to generate -
randomNumberOrigin- the origin (inclusive) of each random value -
randomNumberBound- the bound (exclusive) of each random value - Returns:
- a stream of pseudorandom
intvalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
-
IllegalArgumentException- ifstreamSizeis less than zero, orrandomNumberOriginis greater than or equal torandomNumberBound
ints
public IntStream ints(int randomNumberOrigin, int randomNumberBound)
int values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
-
intsin interfaceRandomGenerator - Implementation Note:
- This method is implemented to be equivalent to
ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
-
randomNumberOrigin- the origin (inclusive) of each random value -
randomNumberBound- the bound (exclusive) of each random value - Returns:
- a stream of pseudorandom
intvalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
-
IllegalArgumentException- ifrandomNumberOriginis greater than or equal torandomNumberBound
longs
public LongStream longs(long streamSize)
streamSize number of pseudorandom long values from this generator and/or one split from it.- Specified by:
-
longsin interfaceRandomGenerator - Parameters:
-
streamSize- the number of values to generate - Returns:
- a stream of pseudorandom
longvalues - Throws:
-
IllegalArgumentException- ifstreamSizeis less than zero
longs
public LongStream longs()
long values from this generator and/or one split from it.- Specified by:
-
longsin interfaceRandomGenerator - Implementation Note:
- This method is implemented to be equivalent to
longs(Long.MAX_VALUE). - Returns:
- a stream of pseudorandom
longvalues
longs
public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
streamSize number of pseudorandom long values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
-
longsin interfaceRandomGenerator - Parameters:
-
streamSize- the number of values to generate -
randomNumberOrigin- the origin (inclusive) of each random value -
randomNumberBound- the bound (exclusive) of each random value - Returns:
- a stream of pseudorandom
longvalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
-
IllegalArgumentException- ifstreamSizeis less than zero, orrandomNumberOriginis greater than or equal torandomNumberBound
longs
public LongStream longs(long randomNumberOrigin, long randomNumberBound)
long values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
-
longsin interfaceRandomGenerator - Implementation Note:
- This method is implemented to be equivalent to
longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
-
randomNumberOrigin- the origin (inclusive) of each random value -
randomNumberBound- the bound (exclusive) of each random value - Returns:
- a stream of pseudorandom
longvalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
-
IllegalArgumentException- ifrandomNumberOriginis greater than or equal torandomNumberBound
doubles
public DoubleStream doubles(long streamSize)
streamSize number of pseudorandom double values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).- Specified by:
-
doublesin interfaceRandomGenerator - Parameters:
-
streamSize- the number of values to generate - Returns:
- a stream of
doublevalues - Throws:
-
IllegalArgumentException- ifstreamSizeis less than zero
doubles
public DoubleStream doubles()
double values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).- Specified by:
-
doublesin interfaceRandomGenerator - Implementation Note:
- This method is implemented to be equivalent to
doubles(Long.MAX_VALUE). - Returns:
- a stream of pseudorandom
doublevalues
doubles
public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
streamSize number of pseudorandom double values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
-
doublesin interfaceRandomGenerator - Parameters:
-
streamSize- the number of values to generate -
randomNumberOrigin- the origin (inclusive) of each random value -
randomNumberBound- the bound (exclusive) of each random value - Returns:
- a stream of pseudorandom
doublevalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
-
IllegalArgumentException- ifstreamSizeis less than zero, orrandomNumberOriginis not finite, orrandomNumberBoundis not finite, orrandomNumberOriginis greater than or equal torandomNumberBound
doubles
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
double values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
-
doublesin interfaceRandomGenerator - Implementation Note:
- This method is implemented to be equivalent to
doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
-
randomNumberOrigin- the origin (inclusive) of each random value -
randomNumberBound- the bound (exclusive) of each random value - Returns:
- a stream of pseudorandom
doublevalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
-
IllegalArgumentException- ifrandomNumberOriginis not finite, orrandomNumberBoundis not finite, orrandomNumberOriginis greater than or equal torandomNumberBound
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.