public final class SplittableRandom extends Object
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.
| Constructor and 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.
|
| Modifier and Type | Method and 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(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).
|
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).
|
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).
|
IntStream |
ints()
Returns an effectively unlimited stream of pseudorandom
int values from this generator and/or one split from it.
|
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).
|
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.
|
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).
|
LongStream |
longs()
Returns an effectively unlimited stream of pseudorandom
long values from this generator and/or one split from it.
|
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.
|
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).
|
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).
|
boolean |
nextBoolean()
Returns a pseudorandom
boolean value.
|
double |
nextDouble()
Returns a pseudorandom
double value between zero (inclusive) and one (exclusive).
|
double |
nextDouble(double bound)
Returns a pseudorandom
double value between 0.0 (inclusive) and the specified bound (exclusive).
|
double |
nextDouble(double origin, double bound)
Returns a pseudorandom
double value between the specified origin (inclusive) and bound (exclusive).
|
int |
nextInt()
Returns a pseudorandom
int value.
|
int |
nextInt(int bound)
Returns a pseudorandom
int value between zero (inclusive) and the specified bound (exclusive).
|
int |
nextInt(int origin, int bound)
Returns a pseudorandom
int value between the specified origin (inclusive) and the specified bound (exclusive).
|
long |
nextLong()
Returns a pseudorandom
long value.
|
long |
nextLong(long bound)
Returns a pseudorandom
long value between zero (inclusive) and the specified bound (exclusive).
|
long |
nextLong(long origin, long bound)
Returns a pseudorandom
long value between the specified origin (inclusive) and the specified bound (exclusive).
|
SplittableRandom |
split()
Constructs and returns a new SplittableRandom instance that shares no mutable state with this instance.
|
public SplittableRandom(long seed)
seed - the initial seed
public SplittableRandom()
public SplittableRandomsplit()
split() method, and the same expected statistical properties apply to the entire set of generators constructed by such recursive splitting.
public int nextInt()
int value.
int value
public int nextInt(int bound)
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 positive
public int nextInt(int origin,
int bound)
int value between the specified origin (inclusive) and the specified bound (exclusive).
origin - the least value returned
bound - 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()
long value.
long value
public long nextLong(long bound)
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 positive
public long nextLong(long origin,
long bound)
long value between the specified origin (inclusive) and the specified bound (exclusive).
origin - the least value returned
bound - 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()
double value between zero (inclusive) and one (exclusive).
double value between zero (inclusive) and one (exclusive)
public double nextDouble(double bound)
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 positive
public double nextDouble(double origin,
double bound)
double value between the specified origin (inclusive) and bound (exclusive).
origin - the least value returned
bound - 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()
boolean value.
boolean value
public IntStreamints(long streamSize)
streamSize number of pseudorandom
int values from this generator and/or one split from it.
streamSize - the number of values to generate
int values
IllegalArgumentException - if
streamSize is less than zero
public IntStreamints()
int values from this generator and/or one split from it.
int values
public IntStreamints(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).
streamSize - the number of values to generate
randomNumberOrigin - the origin (inclusive) of each random value
randomNumberBound - the bound (exclusive) of each random value
int 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 IntStreamints(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).
randomNumberOrigin - the origin (inclusive) of each random value
randomNumberBound - the bound (exclusive) of each random value
int values, each with the given origin (inclusive) and bound (exclusive)
IllegalArgumentException - if
randomNumberOrigin is greater than or equal to
randomNumberBound
public LongStreamlongs(long streamSize)
streamSize number of pseudorandom
long values from this generator and/or one split from it.
streamSize - the number of values to generate
long values
IllegalArgumentException - if
streamSize is less than zero
public LongStreamlongs()
long values from this generator and/or one split from it.
long values
public LongStreamlongs(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).
streamSize - the number of values to generate
randomNumberOrigin - the origin (inclusive) of each random value
randomNumberBound - the bound (exclusive) of each random value
long 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 LongStreamlongs(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).
randomNumberOrigin - the origin (inclusive) of each random value
randomNumberBound - the bound (exclusive) of each random value
long values, each with the given origin (inclusive) and bound (exclusive)
IllegalArgumentException - if
randomNumberOrigin is greater than or equal to
randomNumberBound
public DoubleStreamdoubles(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).
streamSize - the number of values to generate
double values
IllegalArgumentException - if
streamSize is less than zero
public DoubleStreamdoubles()
double values from this generator and/or one split from it; each value is between zero (inclusive) and one (exclusive).
double values
public DoubleStreamdoubles(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).
streamSize - the number of values to generate
randomNumberOrigin - the origin (inclusive) of each random value
randomNumberBound - the bound (exclusive) of each random value
double values, each with the given origin (inclusive) and bound (exclusive)
IllegalArgumentException - if
streamSize is less than zero
IllegalArgumentException - if
randomNumberOrigin is greater than or equal to
randomNumberBound
public DoubleStreamdoubles(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).
randomNumberOrigin - the origin (inclusive) of each random value
randomNumberBound - the bound (exclusive) of each random value
double values, each with the given origin (inclusive) and bound (exclusive)
IllegalArgumentException - if
randomNumberOrigin is greater than or equal to
randomNumberBound