RandomGenerator
, RandomGenerator.SplittableGenerator
, RandomGenerator.StreamableGenerator
public final class SplittableRandom extends Object implements RandomGenerator, RandomGenerator.SplittableGenerator
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
.
RandomGenerator.ArbitrarilyJumpableGenerator, RandomGenerator.JumpableGenerator, RandomGenerator.LeapableGenerator, RandomGenerator.SplittableGenerator, RandomGenerator.StreamableGenerator
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 |
Creates a new SplittableRandom instance using the specified initial seed. |
Modifier and Type | Method | Description |
---|---|---|
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). |
DoubleStream |
doubles |
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). |
DoubleStream |
doubles |
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). |
DoubleStream |
doubles |
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). |
IntStream |
ints() |
Returns an effectively unlimited stream of pseudorandom int values from this generator and/or one split from it. |
IntStream |
ints |
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). |
IntStream |
ints |
Returns a stream producing the given streamSize number of pseudorandom int values from this generator and/or one split from it. |
IntStream |
ints |
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). |
LongStream |
longs() |
Returns an effectively unlimited stream of pseudorandom
long values from this generator and/or one split from it. |
LongStream |
longs |
Returns a stream producing the given streamSize number of pseudorandom long values from this generator and/or one split from it. |
LongStream |
longs |
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). |
LongStream |
longs |
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). |
void |
nextBytes |
Fills a user-supplied byte array with generated byte values pseudorandomly chosen uniformly from the range of values between -128 (inclusive) and 127 (inclusive). |
long |
nextLong() |
Returns a pseudorandomly chosen long value. |
SplittableRandom |
split() |
Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance. |
SplittableRandom |
split |
Returns a new pseudorandom number generator, split off from this one, that implements the RandomGenerator and RandomGenerator.SplittableGenerator interfaces. |
Stream |
splits() |
Returns an effectively unlimited stream of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface. |
Stream |
splits |
Returns a stream producing the given streamSize number of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface. |
Stream |
splits |
Returns a stream producing the given streamSize number of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface. |
Stream |
splits |
Returns an effectively unlimited stream of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator interface. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
isDeprecated, nextBoolean, nextDouble, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextFloat, nextGaussian, nextGaussian, nextInt, nextInt, nextInt, nextLong, nextLong
rngs, rngs
public SplittableRandom(long seed)
seed
- the initial seedpublic SplittableRandom()
public SplittableRandom split()
split()
method, and the same expected statistical properties apply to the entire set of generators constructed by such recursive splitting.split
in interface RandomGenerator.SplittableGenerator
public SplittableRandom split(RandomGenerator.SplittableGenerator source)
RandomGenerator
and RandomGenerator.SplittableGenerator
interfaces.split
in interface RandomGenerator.SplittableGenerator
source
- a RandomGenerator.SplittableGenerator
instance to be used instead of this one as a source of pseudorandom bits used to initialize the state of the new ones.RandomGenerator
and RandomGenerator.SplittableGenerator
interfacesNullPointerException
- if source is nullpublic long nextLong()
RandomGenerator
long
value.nextLong
in interface RandomGenerator
long
valuepublic void nextBytes(byte[] bytes)
nextBytes
in interface RandomGenerator
bytes
- the byte array to fill with pseudorandom bytesNullPointerException
- if bytes is nullpublic Stream<RandomGenerator.SplittableGenerator> splits()
RandomGenerator.SplittableGenerator
interface. This pseudorandom number generator may be used as a source of pseudorandom bits used to initialize the state the new ones.
splits
in interface RandomGenerator.SplittableGenerator
splits(this)
.RandomGenerator.SplittableGenerator
objectspublic Stream<RandomGenerator.SplittableGenerator> splits(long streamSize)
streamSize
number of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator
interface. This pseudorandom number generator may be used as a source of pseudorandom bits used to initialize the state the new ones.
splits
in interface RandomGenerator.SplittableGenerator
streamSize
- the number of values to generateRandomGenerator.SplittableGenerator
objectsIllegalArgumentException
- if streamSize
is less than zeropublic Stream<RandomGenerator.SplittableGenerator> splits(RandomGenerator.SplittableGenerator source)
RandomGenerator.SplittableGenerator
interface.splits
in interface RandomGenerator.SplittableGenerator
source
- a RandomGenerator.SplittableGenerator
instance to be used instead of this one as a source of pseudorandom bits used to initialize the state of the new ones.RandomGenerator.SplittableGenerator
objectsNullPointerException
- if source is nullpublic Stream<RandomGenerator.SplittableGenerator> splits(long streamSize, RandomGenerator.SplittableGenerator source)
streamSize
number of new pseudorandom number generators, each of which implements the RandomGenerator.SplittableGenerator
interface.splits
in interface RandomGenerator.SplittableGenerator
streamSize
- the number of values to generatesource
- a RandomGenerator.SplittableGenerator
instance to be used instead of this one as a source of pseudorandom bits used to initialize the state of the new ones.RandomGenerator.SplittableGenerator
objectsNullPointerException
- if source is nullIllegalArgumentException
- if streamSize
is less than zeropublic IntStream ints(long streamSize)
streamSize
number of pseudorandom int
values from this generator and/or one split from it.ints
in interface RandomGenerator
streamSize
- the number of values to generateint
valuesIllegalArgumentException
- if streamSize
is less than zeropublic IntStream ints()
int
values from this generator and/or one split from it.ints
in interface RandomGenerator
ints(Long.MAX_VALUE)
.int
valuespublic IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
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).ints
in interface RandomGenerator
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)
int
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).ints
in interface RandomGenerator
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)
streamSize
number of pseudorandom long
values from this generator and/or one split from it.longs
in interface RandomGenerator
streamSize
- the number of values to generatelong
valuesIllegalArgumentException
- if streamSize
is less than zeropublic LongStream longs()
long
values from this generator and/or one split from it.longs
in interface RandomGenerator
longs(Long.MAX_VALUE)
.long
valuespublic LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
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).longs
in interface RandomGenerator
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)
long
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).longs
in interface RandomGenerator
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)
streamSize
number of pseudorandom double
values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).doubles
in interface RandomGenerator
streamSize
- the number of values to generatedouble
valuesIllegalArgumentException
- if streamSize
is less than zeropublic DoubleStream doubles()
double
values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).doubles
in interface RandomGenerator
doubles(Long.MAX_VALUE)
.double
valuespublic DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
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).doubles
in interface RandomGenerator
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 zero, or randomNumberOrigin
is not finite, or randomNumberBound
is not finite, or randomNumberOrigin
is greater than or equal to randomNumberBound
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
double
values from this generator and/or one split from it; each value conforms to the given origin (inclusive) and bound (exclusive).doubles
in interface RandomGenerator
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 not finite, or randomNumberBound
is not finite, or randomNumberOrigin
is greater than or equal to randomNumberBound
© 1993, 2023, 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.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/SplittableRandom.html