- All Implemented Interfaces:
-
RandomGenerator
,RandomGenerator.SplittableGenerator
,RandomGenerator.StreamableGenerator
public final class SplittableRandom extends RandomSupport.AbstractSplittableGenerator
A generator of uniform pseudorandom values (with period 264) applicable for use in (among other contexts) isolated parallel computations that may generate subtasks. Class
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 at least 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 singleSplittableRandom
object. -
Instances of SplittableRandom are not thread-safe. They are designed to be split, not shared, across threads. For example, a
fork/join-style
computation 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 java.util.random.RandomGenerator
RandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.RandomGeneratorProperty, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator
-
Field Summary
Fields declared in interface java.util.random.RandomGenerator
HUGE_PERIOD, TRULY_RANDOM, UNKNOWN_PERIOD
-
Constructor Summary
ConstructorsConstructorDescriptionCreates 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(long seed)
Creates a new SplittableRandom instance using the specified initial seed. -
Method Summary
Modifier and TypeMethodDescriptionsplit()
Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance.split(RandomGenerator.SplittableGenerator source)
Returns a new pseudorandom number generator, split off from this one, that implements theRandomGenerator
andRandomGenerator.SplittableGenerator
interfaces.Methods declared in class jdk.internal.util.random.RandomSupport.AbstractSplittableGenerator
splits, splits, splits, splits
Methods declared in class jdk.internal.util.random.RandomSupport.AbstractSpliteratorGenerator
doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, makeDoublesSpliterator, makeIntsSpliterator, makeLongsSpliterator
Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods declared in interface java.util.random.RandomGenerator
doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, nextBoolean, nextBytes, nextDouble, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextFloat, nextGaussian, nextGaussian, nextInt, nextInt, nextInt, nextLong, nextLong, nextLong
Methods declared in interface java.util.random.RandomGenerator.SplittableGenerator
rngs, rngs
-
Constructor Details
-
SplittableRandom
public SplittableRandom(long seed)Creates a new SplittableRandom instance using the specified initial seed. SplittableRandom instances created with the same seed in the same program generate identical sequences of values.- Parameters:
-
seed
- the initial seed
-
SplittableRandom
public 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.
-
-
Method Details
-
split
Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance. However, with very high probability, the set of values collectively generated by the two objects has the same statistical properties as if the same quantity of values were generated by a single thread using a single SplittableRandom object. Either or both of the two objects may be further split using thesplit()
method, and the same expected statistical properties apply to the entire set of generators constructed by such recursive splitting.- Specified by:
-
split
in interfaceRandomGenerator.SplittableGenerator
- Overrides:
-
split
in classRandomSupport.AbstractSplittableGenerator
- Returns:
- the new SplittableRandom instance
-
split
Description copied from interface:RandomGenerator.SplittableGenerator
Returns a new pseudorandom number generator, split off from this one, that implements theRandomGenerator
andRandomGenerator.SplittableGenerator
interfaces.- Parameters:
-
source
- aRandomGenerator.SplittableGenerator
instance 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
RandomGenerator
andRandomGenerator.SplittableGenerator
interfaces
-