java.lang.Object
java.util.SplittableRandom
- All Implemented Interfaces:
- RandomGenerator,- RandomGenerator.SplittableGenerator,- RandomGenerator.StreamableGenerator
public final class SplittableRandom
extends Object
implements RandomGenerator, RandomGenerator.SplittableGenerator
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 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 SummaryNested classes/interfaces declared in interface java.util.random.RandomGeneratorRandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator
- 
Constructor SummaryConstructorsConstructorDescriptionCreates 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 SummaryModifier and TypeMethodDescriptiondoubles()Returns an effectively unlimited stream of pseudorandomdoublevalues from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).doubles(double randomNumberOrigin, double randomNumberBound) Returns an effectively unlimited stream of pseudorandomdoublevalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).doubles(long streamSize) Returns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) Returns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).ints()Returns an effectively unlimited stream of pseudorandomintvalues from this generator and/or one split from it.ints(int randomNumberOrigin, int randomNumberBound) Returns an effectively unlimited stream of pseudorandomintvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).ints(long streamSize) Returns a stream producing the givenstreamSizenumber of pseudorandomintvalues from this generator and/or one split from it.ints(long streamSize, int randomNumberOrigin, int randomNumberBound) Returns a stream producing the givenstreamSizenumber of pseudorandomintvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).longs()Returns an effectively unlimited stream of pseudorandomlongvalues from this generator and/or one split from it.longs(long streamSize) Returns a stream producing the givenstreamSizenumber of pseudorandomlongvalues from this generator and/or one split from it.longs(long randomNumberOrigin, long randomNumberBound) Returns an effectively unlimited stream of pseudorandomlongvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).longs(long streamSize, long randomNumberOrigin, long randomNumberBound) Returns a stream producing the givenstreamSizenumber of pseudorandomlongvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).voidnextBytes(byte[] 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).longnextLong()Returns a pseudorandomly chosenlongvalue.split()Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance.Returns a new pseudorandom number generator, split off from this one, that implements theRandomGeneratorandRandomGenerator.SplittableGeneratorinterfaces.splits()Returns an effectively unlimited stream of new pseudorandom number generators, each of which implements theRandomGenerator.SplittableGeneratorinterface.splits(long streamSize) Returns a stream producing the givenstreamSizenumber of new pseudorandom number generators, each of which implements theRandomGenerator.SplittableGeneratorinterface.splits(long streamSize, RandomGenerator.SplittableGenerator source) Returns a stream producing the givenstreamSizenumber of new pseudorandom number generators, each of which implements theRandomGenerator.SplittableGeneratorinterface.Returns an effectively unlimited stream of new pseudorandom number generators, each of which implements theRandomGenerator.SplittableGeneratorinterface.Methods declared in class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods declared in interface java.util.random.RandomGeneratorisDeprecated, nextBoolean, nextDouble, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextFloat, nextGaussian, nextGaussian, nextInt, nextInt, nextInt, nextLong, nextLongMethods declared in interface java.util.random.RandomGenerator.SplittableGeneratorrngs, rngs
- 
Constructor Details- 
SplittableRandompublic 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
 
- 
SplittableRandompublic 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- 
splitConstructs 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:
- splitin interface- RandomGenerator.SplittableGenerator
- Returns:
- the new SplittableRandom instance
 
- 
splitReturns a new pseudorandom number generator, split off from this one, that implements theRandomGeneratorandRandomGenerator.SplittableGeneratorinterfaces.- Specified by:
- splitin interface- RandomGenerator.SplittableGenerator
- Parameters:
- source- a- RandomGenerator.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
 
- 
nextLongpublic long nextLong()Description copied from interface:RandomGeneratorReturns a pseudorandomly chosenlongvalue.- Specified by:
- nextLongin interface- RandomGenerator
- Returns:
- a pseudorandomly chosen longvalue
 
- 
nextBytespublic void nextBytes(byte[] 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).- Specified by:
- nextBytesin interface- RandomGenerator
- Parameters:
- bytes- the byte array to fill with pseudorandom bytes
- Throws:
- NullPointerException- if bytes is null
- Since:
- 10
 
- 
splitsReturns an effectively unlimited stream of new pseudorandom number generators, each of which implements theRandomGenerator.SplittableGeneratorinterface.This pseudorandom number generator may be used as a source of pseudorandom bits used to initialize the state the new ones. - Specified by:
- splitsin interface- RandomGenerator.SplittableGenerator
- Implementation Requirements:
- The default implementation invokes
 splits(this).
- Returns:
- a stream of RandomGenerator.SplittableGeneratorobjects
- Since:
- 17
 
- 
splitsReturns a stream producing the givenstreamSizenumber of new pseudorandom number generators, each of which implements theRandomGenerator.SplittableGeneratorinterface.This pseudorandom number generator may be used as a source of pseudorandom bits used to initialize the state the new ones. - Specified by:
- splitsin interface- RandomGenerator.SplittableGenerator
- Implementation Requirements:
- Parameters:
- streamSize- the number of values to generate
- Returns:
- a stream of RandomGenerator.SplittableGeneratorobjects
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero
- Since:
- 17
 
- 
splitspublic Stream<RandomGenerator.SplittableGenerator> splits(RandomGenerator.SplittableGenerator source) Returns an effectively unlimited stream of new pseudorandom number generators, each of which implements theRandomGenerator.SplittableGeneratorinterface.- Specified by:
- splitsin interface- RandomGenerator.SplittableGenerator
- Implementation Requirements:
- Parameters:
- source- a- RandomGenerator.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
 
- 
splitspublic Stream<RandomGenerator.SplittableGenerator> splits(long streamSize, RandomGenerator.SplittableGenerator source) Returns a stream producing the givenstreamSizenumber of new pseudorandom number generators, each of which implements theRandomGenerator.SplittableGeneratorinterface.- Specified by:
- splitsin interface- RandomGenerator.SplittableGenerator
- Implementation Requirements:
- Parameters:
- streamSize- the number of values to generate
- source- a- RandomGenerator.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- if- streamSizeis less than zero
- Since:
- 17
 
- 
intsReturns a stream producing the givenstreamSizenumber of pseudorandomintvalues from this generator and/or one split from it.- Specified by:
- intsin interface- RandomGenerator
- Parameters:
- streamSize- the number of values to generate
- Returns:
- a stream of pseudorandom intvalues
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero
 
- 
intsReturns an effectively unlimited stream of pseudorandomintvalues from this generator and/or one split from it.- Specified by:
- intsin interface- RandomGenerator
- Implementation Note:
- This method is implemented to be equivalent to ints(Long.MAX_VALUE).
- Returns:
- a stream of pseudorandom intvalues
 
- 
intsReturns a stream producing the givenstreamSizenumber of pseudorandomintvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
- intsin interface- RandomGenerator
- 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- if- streamSizeis less than zero, or- randomNumberOriginis greater than or equal to- randomNumberBound
 
- 
intsReturns an effectively unlimited stream of pseudorandomintvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
- intsin interface- RandomGenerator
- 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- if- randomNumberOriginis greater than or equal to- randomNumberBound
 
- 
longsReturns a stream producing the givenstreamSizenumber of pseudorandomlongvalues from this generator and/or one split from it.- Specified by:
- longsin interface- RandomGenerator
- Parameters:
- streamSize- the number of values to generate
- Returns:
- a stream of pseudorandom longvalues
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero
 
- 
longsReturns an effectively unlimited stream of pseudorandomlongvalues from this generator and/or one split from it.- Specified by:
- longsin interface- RandomGenerator
- Implementation Note:
- This method is implemented to be equivalent to longs(Long.MAX_VALUE).
- Returns:
- a stream of pseudorandom longvalues
 
- 
longsReturns a stream producing the givenstreamSizenumber of pseudorandomlongvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
- longsin interface- RandomGenerator
- 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- if- streamSizeis less than zero, or- randomNumberOriginis greater than or equal to- randomNumberBound
 
- 
longsReturns an effectively unlimited stream of pseudorandomlongvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
- longsin interface- RandomGenerator
- 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- if- randomNumberOriginis greater than or equal to- randomNumberBound
 
- 
doublesReturns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).- Specified by:
- doublesin interface- RandomGenerator
- Parameters:
- streamSize- the number of values to generate
- Returns:
- a stream of doublevalues
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero
 
- 
doublesReturns an effectively unlimited stream of pseudorandomdoublevalues from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).- Specified by:
- doublesin interface- RandomGenerator
- Implementation Note:
- This method is implemented to be equivalent to doubles(Long.MAX_VALUE).
- Returns:
- a stream of pseudorandom doublevalues
 
- 
doublesReturns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
- doublesin interface- RandomGenerator
- 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- if- streamSizeis less than zero, or- randomNumberOriginis not finite, or- randomNumberBoundis not finite, or- randomNumberOriginis greater than or equal to- randomNumberBound
 
- 
doublesReturns an effectively unlimited stream of pseudorandomdoublevalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Specified by:
- doublesin interface- RandomGenerator
- 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- if- randomNumberOriginis not finite, or- randomNumberBoundis not finite, or- randomNumberOriginis greater than or equal to- randomNumberBound
 
 
-