SplittableRandom (Java SE 13 & JDK 13 )
public final class SplittableRandom extends Object
A generator of uniform pseudorandom values 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 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
-
Constructor Summary
Constructors 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(long seed)Creates a new SplittableRandom instance using the specified initial seed.
-
Method Summary
Modifier and Type Method Description DoubleStreamdoubles()Returns an effectively unlimited stream of pseudorandom
doublevalues from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).DoubleStreamdoubles(double randomNumberOrigin, double randomNumberBound)Returns an effectively unlimited stream of pseudorandom
doublevalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).DoubleStreamdoubles(long streamSize)Returns a stream producing the given
streamSizenumber of pseudorandomdoublevalues from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).DoubleStreamdoubles(long streamSize, double randomNumberOrigin, double randomNumberBound)Returns a stream producing the given
streamSizenumber of pseudorandomdoublevalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).IntStreamints()Returns an effectively unlimited stream of pseudorandom
intvalues from this generator and/or one split from it.IntStreamints(int randomNumberOrigin, int randomNumberBound)Returns an effectively unlimited stream of pseudorandom
intvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).IntStreamints(long streamSize)Returns a stream producing the given
streamSizenumber of pseudorandomintvalues from this generator and/or one split from it.IntStreamints(long streamSize, int randomNumberOrigin, int randomNumberBound)Returns a stream producing the given
streamSizenumber of pseudorandomintvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).LongStreamlongs()Returns an effectively unlimited stream of pseudorandom
longvalues from this generator and/or one split from it.LongStreamlongs(long streamSize)Returns a stream producing the given
streamSizenumber of pseudorandomlongvalues from this generator and/or one split from it.LongStreamlongs(long randomNumberOrigin, long randomNumberBound)Returns an effectively unlimited stream of pseudorandom
longvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).LongStreamlongs(long streamSize, long randomNumberOrigin, long randomNumberBound)Returns a stream producing the given
streamSizenumber of pseudorandomlongvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).booleannextBoolean()Returns a pseudorandom
booleanvalue.voidnextBytes(byte[] bytes)Fills a user-supplied byte array with generated pseudorandom bytes.
doublenextDouble()Returns a pseudorandom
doublevalue between zero (inclusive) and one (exclusive).doublenextDouble(double bound)Returns a pseudorandom
doublevalue between 0.0 (inclusive) and the specified bound (exclusive).doublenextDouble(double origin, double bound)Returns a pseudorandom
doublevalue between the specified origin (inclusive) and bound (exclusive).intnextInt()Returns a pseudorandom
intvalue.intnextInt(int bound)Returns a pseudorandom
intvalue between zero (inclusive) and the specified bound (exclusive).intnextInt(int origin, int bound)Returns a pseudorandom
intvalue between the specified origin (inclusive) and the specified bound (exclusive).longnextLong()Returns a pseudorandom
longvalue.longnextLong(long bound)Returns a pseudorandom
longvalue between zero (inclusive) and the specified bound (exclusive).longnextLong(long origin, long bound)Returns a pseudorandom
longvalue between the specified origin (inclusive) and the specified bound (exclusive).SplittableRandomsplit()Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance.
-
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 the
split()method, and the same expected statistical properties apply to the entire set of generators constructed by such recursive splitting.- Returns:
- the new SplittableRandom instance
-
nextBytes
public void nextBytes(byte[] bytes)
Fills a user-supplied byte array with generated pseudorandom bytes.
- Parameters:
bytes- the byte array to fill with pseudorandom bytes- Throws:
NullPointerException- if bytes is null- Since:
- 10
-
nextInt
public int nextInt()
Returns a pseudorandom
intvalue.- Returns:
- a pseudorandom
intvalue
-
nextInt
public int nextInt(int bound)
Returns a pseudorandom
intvalue between zero (inclusive) and the specified bound (exclusive).- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
intvalue between zero (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- ifboundis not positive
-
nextInt
public int nextInt(int origin, int bound)
Returns a pseudorandom
intvalue between the specified origin (inclusive) and the specified bound (exclusive).- Parameters:
origin- the least value returnedbound- the upper bound (exclusive)- Returns:
- a pseudorandom
intvalue between the origin (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- iforiginis greater than or equal tobound
-
nextLong
public long nextLong()
Returns a pseudorandom
longvalue.- Returns:
- a pseudorandom
longvalue
-
nextLong
public long nextLong(long bound)
Returns a pseudorandom
longvalue between zero (inclusive) and the specified bound (exclusive).- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
longvalue between zero (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- ifboundis not positive
-
nextLong
public long nextLong(long origin, long bound)
Returns a pseudorandom
longvalue between the specified origin (inclusive) and the specified bound (exclusive).- Parameters:
origin- the least value returnedbound- the upper bound (exclusive)- Returns:
- a pseudorandom
longvalue between the origin (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- iforiginis greater than or equal tobound
-
nextDouble
public double nextDouble()
Returns a pseudorandom
doublevalue between zero (inclusive) and one (exclusive).- Returns:
- a pseudorandom
doublevalue between zero (inclusive) and one (exclusive)
-
nextDouble
public double nextDouble(double bound)
Returns a pseudorandom
doublevalue between 0.0 (inclusive) and the specified bound (exclusive).- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
doublevalue between zero (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- ifboundis not positive
-
nextDouble
public double nextDouble(double origin, double bound)
Returns a pseudorandom
doublevalue between the specified origin (inclusive) and bound (exclusive).- Parameters:
origin- the least value returnedbound- the upper bound (exclusive)- Returns:
- a pseudorandom
doublevalue between the origin (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- iforiginis greater than or equal tobound
-
nextBoolean
public boolean nextBoolean()
Returns a pseudorandom
booleanvalue.- Returns:
- a pseudorandom
booleanvalue
-
ints
public IntStream ints(long streamSize)
Returns a stream producing the given
streamSizenumber of pseudorandomintvalues from this generator and/or one split from it.- Parameters:
streamSize- the number of values to generate- Returns:
- a stream of pseudorandom
intvalues - Throws:
IllegalArgumentException- ifstreamSizeis less than zero
-
ints
Returns an effectively unlimited stream of pseudorandom
intvalues from this generator and/or one split from it.- 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)
Returns a stream producing the given
streamSizenumber of pseudorandomintvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Parameters:
streamSize- the number of values to generaterandomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- 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)
Returns an effectively unlimited stream of pseudorandom
intvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Implementation Note:
- This method is implemented to be equivalent to
ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
randomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- 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)
Returns a stream producing the given
streamSizenumber of pseudorandomlongvalues from this generator and/or one split from it.- Parameters:
streamSize- the number of values to generate- Returns:
- a stream of pseudorandom
longvalues - Throws:
IllegalArgumentException- ifstreamSizeis less than zero
-
longs
Returns an effectively unlimited stream of pseudorandom
longvalues from this generator and/or one split from it.- 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)
Returns a stream producing the given
streamSizenumber of pseudorandomlongvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Parameters:
streamSize- the number of values to generaterandomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- 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)
Returns an effectively unlimited stream of pseudorandom
longvalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Implementation Note:
- This method is implemented to be equivalent to
longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
randomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- 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
Returns a stream producing the given
streamSizenumber of pseudorandomdoublevalues from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).- Parameters:
streamSize- the number of values to generate- Returns:
- a stream of
doublevalues - Throws:
IllegalArgumentException- ifstreamSizeis less than zero
-
doubles
Returns an effectively unlimited stream of pseudorandom
doublevalues from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).- 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)
Returns a stream producing the given
streamSizenumber of pseudorandomdoublevalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Parameters:
streamSize- the number of values to generaterandomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- 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 greater than or equal torandomNumberBound
-
doubles
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
Returns an effectively unlimited stream of pseudorandom
doublevalues from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Implementation Note:
- This method is implemented to be equivalent to
doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
randomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- 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 greater than or equal torandomNumberBound
-
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2019, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.