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:
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 single SplittableRandom
object. fork/join-style
computation using random numbers might include a construction of the form new
Subtask(aSplittableRandom.split()).fork()
. 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
.
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.
seed
- the initial seedpublic 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.
public SplittableRandom 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.
public int nextInt()
Returns a pseudorandom int
value.
int
valuepublic int nextInt(int bound)
Returns a pseudorandom int
value between zero (inclusive) and the specified bound (exclusive).
bound
- the upper bound (exclusive). Must be positive.int
value between zero (inclusive) and the bound (exclusive)IllegalArgumentException
- if bound
is not positivepublic int nextInt(int origin, int bound)
Returns a pseudorandom int
value between the specified origin (inclusive) and the specified bound (exclusive).
origin
- the least value returnedbound
- the upper bound (exclusive)int
value between the origin (inclusive) and the bound (exclusive)IllegalArgumentException
- if origin
is greater than or equal to bound
public long nextLong()
Returns a pseudorandom long
value.
long
valuepublic long nextLong(long bound)
Returns a pseudorandom long
value between zero (inclusive) and the specified bound (exclusive).
bound
- the upper bound (exclusive). Must be positive.long
value between zero (inclusive) and the bound (exclusive)IllegalArgumentException
- if bound
is not positivepublic long nextLong(long origin, long bound)
Returns a pseudorandom long
value between the specified origin (inclusive) and the specified bound (exclusive).
origin
- the least value returnedbound
- the upper bound (exclusive)long
value between the origin (inclusive) and the bound (exclusive)IllegalArgumentException
- if origin
is greater than or equal to bound
public double nextDouble()
Returns a pseudorandom double
value between zero (inclusive) and one (exclusive).
double
value between zero (inclusive) and one (exclusive)public double nextDouble(double bound)
Returns a pseudorandom double
value between 0.0 (inclusive) and the specified bound (exclusive).
bound
- the upper bound (exclusive). Must be positive.double
value between zero (inclusive) and the bound (exclusive)IllegalArgumentException
- if bound
is not positivepublic double nextDouble(double origin, double bound)
Returns a pseudorandom double
value between the specified origin (inclusive) and bound (exclusive).
origin
- the least value returnedbound
- the upper bound (exclusive)double
value between the origin (inclusive) and the bound (exclusive)IllegalArgumentException
- if origin
is greater than or equal to bound
public boolean nextBoolean()
Returns a pseudorandom boolean
value.
boolean
valuepublic IntStream ints(long streamSize)
Returns a stream producing the given streamSize
number of pseudorandom int
values from this generator and/or one split from it.
streamSize
- the number of values to generateint
valuesIllegalArgumentException
- if streamSize
is less than zeropublic IntStream ints()
Returns an effectively unlimited stream of pseudorandom int
values from this generator and/or one split from it.
ints(Long.MAX_VALUE)
.int
valuespublic IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
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).
streamSize
- the number of values to generaterandomNumberOrigin
- the origin (inclusive) of each random valuerandomNumberBound
- the bound (exclusive) of each random valueint
values, each with the given origin (inclusive) and bound (exclusive)IllegalArgumentException
- if streamSize
is less than zero, or randomNumberOrigin
is greater than or equal to randomNumberBound
public IntStream ints(int randomNumberOrigin, int randomNumberBound)
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).
ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)
.randomNumberOrigin
- the origin (inclusive) of each random valuerandomNumberBound
- the bound (exclusive) of each random valueint
values, each with the given origin (inclusive) and bound (exclusive)IllegalArgumentException
- if randomNumberOrigin
is greater than or equal to randomNumberBound
public LongStream longs(long streamSize)
Returns a stream producing the given streamSize
number of pseudorandom long
values from this generator and/or one split from it.
streamSize
- the number of values to generatelong
valuesIllegalArgumentException
- if streamSize
is less than zeropublic LongStream longs()
Returns an effectively unlimited stream of pseudorandom long
values from this generator and/or one split from it.
longs(Long.MAX_VALUE)
.long
valuespublic LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
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).
streamSize
- the number of values to generaterandomNumberOrigin
- the origin (inclusive) of each random valuerandomNumberBound
- the bound (exclusive) of each random valuelong
values, each with the given origin (inclusive) and bound (exclusive)IllegalArgumentException
- if streamSize
is less than zero, or randomNumberOrigin
is greater than or equal to randomNumberBound
public LongStream longs(long randomNumberOrigin, long randomNumberBound)
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).
longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)
.randomNumberOrigin
- the origin (inclusive) of each random valuerandomNumberBound
- the bound (exclusive) of each random valuelong
values, each with the given origin (inclusive) and bound (exclusive)IllegalArgumentException
- if randomNumberOrigin
is greater than or equal to randomNumberBound
public DoubleStream doubles(long streamSize)
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).
streamSize
- the number of values to generatedouble
valuesIllegalArgumentException
- if streamSize
is less than zeropublic 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).
doubles(Long.MAX_VALUE)
.double
valuespublic DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
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).
streamSize
- the number of values to generaterandomNumberOrigin
- the origin (inclusive) of each random valuerandomNumberBound
- the bound (exclusive) of each random valuedouble
values, each with the given origin (inclusive) and bound (exclusive)IllegalArgumentException
- if streamSize
is less than zeroIllegalArgumentException
- if randomNumberOrigin
is greater than or equal to randomNumberBound
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
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).
doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)
.randomNumberOrigin
- the origin (inclusive) of each random valuerandomNumberBound
- the bound (exclusive) of each random valuedouble
values, each with the given origin (inclusive) and bound (exclusive)IllegalArgumentException
- if randomNumberOrigin
is greater than or equal to randomNumberBound
© 1993–2017, 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.