Package HAL.Tools.Internal
Class SplittableRN
- java.lang.Object
-
- HAL.Tools.Internal.SplittableRN
-
- All Implemented Interfaces:
RNG
,java.io.Serializable
public final class SplittableRN extends java.lang.Object implements java.io.Serializable, RNG
A generator of uniform pseudorandom values applicable for use in (among other contexts) isolated parallel computations that may generate subtasks. ClassSplittableRandom
supports methods for producing pseudorandom numbers of typeint
,long
, anddouble
with similar usages as for classRandom
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 usingSecureRandom
in security-sensitive applications. Additionally, default-constructed instances do not use a cryptographically random seed unless the system propertyjava.util.secureRandomSeed
is set totrue
.- Since:
- 1.8
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SplittableRN()
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.SplittableRN(long seed)
Creates a new SplittableRandom instance using the specified initial seed.
-
Method Summary
Modifier and Type Method Description boolean
Bool()
double
Double()
double
Double(double bound)
java.util.stream.DoubleStream
doubles()
Returns an effectively unlimited stream of pseudorandomdouble
values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).java.util.stream.DoubleStream
doubles(double randomNumberOrigin, double randomNumberBound)
Returns an effectively unlimited stream of pseudorandomdouble
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).java.util.stream.DoubleStream
doubles(long streamSize)
Returns a stream producing the givenstreamSize
number of pseudorandomdouble
values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).java.util.stream.DoubleStream
doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
Returns a stream producing the givenstreamSize
number of pseudorandomdouble
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).int
Int(int bound)
java.util.stream.IntStream
ints()
Returns an effectively unlimited stream of pseudorandomint
values from this generator and/or one split from it.java.util.stream.IntStream
ints(int randomNumberOrigin, int randomNumberBound)
Returns an effectively unlimited stream of pseudorandomint
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).java.util.stream.IntStream
ints(long streamSize)
Returns a stream producing the givenstreamSize
number of pseudorandomint
values from this generator and/or one split from it.java.util.stream.IntStream
ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Returns a stream producing the givenstreamSize
number of pseudorandomint
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).long
Long(long bound)
java.util.stream.LongStream
longs()
Returns an effectively unlimited stream of pseudorandomlong
values from this generator and/or one split from it.java.util.stream.LongStream
longs(long streamSize)
Returns a stream producing the givenstreamSize
number of pseudorandomlong
values from this generator and/or one split from it.java.util.stream.LongStream
longs(long randomNumberOrigin, long randomNumberBound)
Returns an effectively unlimited stream of pseudorandomlong
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).java.util.stream.LongStream
longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
Returns a stream producing the givenstreamSize
number of pseudorandomlong
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).boolean
nextBoolean()
Returns a pseudorandomboolean
value.double
nextDouble()
Returns a pseudorandomdouble
value between zero (inclusive) and one (exclusive).double
nextDouble(double bound)
Returns a pseudorandomdouble
value between 0.0 (inclusive) and the specified bound (exclusive).double
nextDouble(double origin, double bound)
Returns a pseudorandomdouble
value between the specified origin (inclusive) and bound (exclusive).int
nextInt()
Returns a pseudorandomint
value.int
nextInt(int bound)
Returns a pseudorandomint
value between zero (inclusive) and the specified bound (exclusive).int
nextInt(int origin, int bound)
Returns a pseudorandomint
value between the specified origin (inclusive) and the specified bound (exclusive).long
nextLong()
Returns a pseudorandomlong
value.long
nextLong(long bound)
Returns a pseudorandomlong
value between zero (inclusive) and the specified bound (exclusive).long
nextLong(long origin, long bound)
Returns a pseudorandomlong
value between the specified origin (inclusive) and the specified bound (exclusive).SplittableRN
split()
Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance.
-
-
-
Constructor Detail
-
SplittableRN
public SplittableRN(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
-
SplittableRN
public SplittableRN()
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 Detail
-
split
public SplittableRN 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.- Returns:
- the new SplittableRandom instance
-
nextInt
public int nextInt()
Returns a pseudorandomint
value.- Returns:
- a pseudorandom
int
value
-
nextInt
public int nextInt(int bound)
Returns a pseudorandomint
value between zero (inclusive) and the specified bound (exclusive).- Parameters:
bound
- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
int
value between zero (inclusive) and the bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- ifbound
is not positive
-
nextInt
public int nextInt(int origin, int bound)
Returns a pseudorandomint
value between the specified origin (inclusive) and the specified bound (exclusive).- Parameters:
origin
- the least value returnedbound
- the upper bound (exclusive)- Returns:
- a pseudorandom
int
value between the origin (inclusive) and the bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- iforigin
is greater than or equal tobound
-
nextLong
public long nextLong()
Returns a pseudorandomlong
value.- Returns:
- a pseudorandom
long
value
-
nextLong
public long nextLong(long bound)
Returns a pseudorandomlong
value between zero (inclusive) and the specified bound (exclusive).- Parameters:
bound
- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
long
value between zero (inclusive) and the bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- ifbound
is not positive
-
nextLong
public long nextLong(long origin, long bound)
Returns a pseudorandomlong
value between the specified origin (inclusive) and the specified bound (exclusive).- Parameters:
origin
- the least value returnedbound
- the upper bound (exclusive)- Returns:
- a pseudorandom
long
value between the origin (inclusive) and the bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- iforigin
is greater than or equal tobound
-
nextDouble
public double nextDouble()
Returns a pseudorandomdouble
value between zero (inclusive) and one (exclusive).- Returns:
- a pseudorandom
double
value between zero (inclusive) and one (exclusive)
-
nextDouble
public double nextDouble(double bound)
Returns a pseudorandomdouble
value between 0.0 (inclusive) and the specified bound (exclusive).- Parameters:
bound
- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
double
value between zero (inclusive) and the bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- ifbound
is not positive
-
nextDouble
public double nextDouble(double origin, double bound)
Returns a pseudorandomdouble
value between the specified origin (inclusive) and bound (exclusive).- Parameters:
origin
- the least value returnedbound
- the upper bound (exclusive)- Returns:
- a pseudorandom
double
value between the origin (inclusive) and the bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- iforigin
is greater than or equal tobound
-
nextBoolean
public boolean nextBoolean()
Returns a pseudorandomboolean
value.- Returns:
- a pseudorandom
boolean
value
-
ints
public java.util.stream.IntStream ints(long streamSize)
Returns a stream producing the givenstreamSize
number of pseudorandomint
values from this generator and/or one split from it.- Parameters:
streamSize
- the number of values to generate- Returns:
- a stream of pseudorandom
int
values - Throws:
java.lang.IllegalArgumentException
- ifstreamSize
is less than zero
-
ints
public java.util.stream.IntStream ints()
Returns an effectively unlimited stream of pseudorandomint
values from this generator and/or one split from it.- Returns:
- a stream of pseudorandom
int
values
-
ints
public java.util.stream.IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Returns a stream producing the givenstreamSize
number of pseudorandomint
values 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
int
values, each with the given origin (inclusive) and bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- ifstreamSize
is less than zero, orrandomNumberOrigin
is greater than or equal torandomNumberBound
-
ints
public java.util.stream.IntStream ints(int randomNumberOrigin, int randomNumberBound)
Returns an effectively unlimited stream of pseudorandomint
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Parameters:
randomNumberOrigin
- the origin (inclusive) of each random valuerandomNumberBound
- the bound (exclusive) of each random value- Returns:
- a stream of pseudorandom
int
values, each with the given origin (inclusive) and bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- ifrandomNumberOrigin
is greater than or equal torandomNumberBound
-
longs
public java.util.stream.LongStream longs(long streamSize)
Returns a stream producing the givenstreamSize
number of pseudorandomlong
values from this generator and/or one split from it.- Parameters:
streamSize
- the number of values to generate- Returns:
- a stream of pseudorandom
long
values - Throws:
java.lang.IllegalArgumentException
- ifstreamSize
is less than zero
-
longs
public java.util.stream.LongStream longs()
Returns an effectively unlimited stream of pseudorandomlong
values from this generator and/or one split from it.- Returns:
- a stream of pseudorandom
long
values
-
longs
public java.util.stream.LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
Returns a stream producing the givenstreamSize
number of pseudorandomlong
values 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
long
values, each with the given origin (inclusive) and bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- ifstreamSize
is less than zero, orrandomNumberOrigin
is greater than or equal torandomNumberBound
-
longs
public java.util.stream.LongStream longs(long randomNumberOrigin, long randomNumberBound)
Returns an effectively unlimited stream of pseudorandomlong
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Parameters:
randomNumberOrigin
- the origin (inclusive) of each random valuerandomNumberBound
- the bound (exclusive) of each random value- Returns:
- a stream of pseudorandom
long
values, each with the given origin (inclusive) and bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- ifrandomNumberOrigin
is greater than or equal torandomNumberBound
-
doubles
public java.util.stream.DoubleStream doubles(long streamSize)
Returns a stream producing the givenstreamSize
number of pseudorandomdouble
values 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
double
values - Throws:
java.lang.IllegalArgumentException
- ifstreamSize
is less than zero
-
doubles
public java.util.stream.DoubleStream doubles()
Returns an effectively unlimited stream of pseudorandomdouble
values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).- Returns:
- a stream of pseudorandom
double
values
-
doubles
public java.util.stream.DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
Returns a stream producing the givenstreamSize
number of pseudorandomdouble
values 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
double
values, each with the given origin (inclusive) and bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- ifstreamSize
is less than zero, orrandomNumberOrigin
is greater than or equal torandomNumberBound
-
doubles
public java.util.stream.DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
Returns an effectively unlimited stream of pseudorandomdouble
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).- Parameters:
randomNumberOrigin
- the origin (inclusive) of each random valuerandomNumberBound
- the bound (exclusive) of each random value- Returns:
- a stream of pseudorandom
double
values, each with the given origin (inclusive) and bound (exclusive) - Throws:
java.lang.IllegalArgumentException
- ifrandomNumberOrigin
is greater than or equal torandomNumberBound
-
-