SplittableRandom
public final class SplittableRandom
extends Object
一个统一的伪随机值生成器,适用于(在其他上下文中)用于隔离的并行计算,这些计算可能会生成子任务。 类SplittableRandom
支持方法用于生产类型的伪随机数int
, long
和double
具有类似用途作为类Random
但在以下方面不同:
- 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 single SplittableRandom
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 form new Subtask(aSplittableRandom.split()).fork()
.
- This class provides additional methods for generating random streams, that employ the above techniques when used in
stream.parallel()
mode.
SplittableRandom
实例不具有密码安全性。 请考虑在安全敏感的应用程序中使用SecureRandom
。 此外,除非system property java.util.secureRandomSeed
设置为true
否则默认构建的实例不使用加密随机种子。
Summary
Public constructors |
SplittableRandom(long seed) 使用指定的初始种子创建一个新的SplittableRandom实例。 |
SplittableRandom() 创建一个新的SplittableRandom实例,该实例可能会生成统计独立于当前程序中任何其他实例的值的序列值; 并且可能并且通常确实会因程序调用而异。 |
Public methods |
DoubleStream |
doubles(long streamSize) 返回产生所述给定流streamSize 数的伪随机的double 从该发电机和/或从它一个分割值; 每个值在零(包括)和一个(独占)之间。 |
DoubleStream |
doubles() 返回来自此生成器的伪随机数double 的有效无限数据流和/或从其中分离出一个; 每个值在零(包括)和一个(独占)之间。 |
DoubleStream |
doubles(double randomNumberOrigin, double randomNumberBound) 返回来自此生成器的伪随机数double 的有效无限流和/或从其中分离出的一个; 每个值符合给定的原点(包括)和绑定(独占)。 |
DoubleStream |
doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) 返回产生所述给定流streamSize 数的伪随机的double 从该发电机和/或从它一个分割值; 每个值符合给定的原点(包括)和绑定(独占)。 |
IntStream |
ints(long streamSize) 返回产生所述给定流 streamSize 数的伪随机的 int 从该发电机和/或从它一个分割值。 |
IntStream |
ints(long streamSize, int randomNumberOrigin, int randomNumberBound) 返回产生所述给定流streamSize 数的伪随机的int 从该发电机和/或从它一个分割值; 每个值符合给定的原点(包括)和绑定(独占)。 |
IntStream |
ints(int randomNumberOrigin, int randomNumberBound) 返回来自该生成器的伪随机数int 的有效无限流和/或从其中分离出一个; 每个值符合给定的原点(包括)和绑定(独占)。 |
IntStream |
ints() 返回来自此生成器的伪随机值 int 的有效无限流和/或从其中分离出的一个值。 |
LongStream |
longs() 返回来自此生成器的伪随机数 long 的有效无限数据流和/或从其中分离出一个。 |
LongStream |
longs(long streamSize) 返回产生所述给定流 streamSize 数的伪随机的 long 从该发电机和/或从它一个分割值。 |
LongStream |
longs(long randomNumberOrigin, long randomNumberBound) 返回来自此生成器的伪随机数long 的有效无限流和/或从其中分离出的一个; 每个值符合给定的原点(包括)和绑定(独占)。 |
LongStream |
longs(long streamSize, long randomNumberOrigin, long randomNumberBound) 返回产生所述给定流streamSize 数的伪随机的long 从该发电机和/或从它一个分割值; 每个值符合给定的原点(包括)和绑定(独占)。 |
boolean |
nextBoolean() 返回伪随机值 boolean 值。 |
double |
nextDouble() 返回零(含)和一(独占)之间的伪随机值 double 。 |
double |
nextDouble(double bound) 返回0.0(包含)与指定的边界(不包括)之间的伪随机数 double 。 |
double |
nextDouble(double origin, double bound) 返回指定原点(包含)和绑定(独占)之间的伪随机值 double 。 |
int |
nextInt() 返回伪随机值 int 值。 |
int |
nextInt(int origin, int bound) 返回指定原点(包含)与指定的边界(不包括)之间的伪随机值 int 。 |
int |
nextInt(int bound) 返回0(包含)和指定的边界(独占)之间的伪随机值 int 。 |
long |
nextLong(long bound) 返回零(包括)与指定的边界(独占)之间的伪随机值 long 。 |
long |
nextLong() 返回伪随机值 long 值。 |
long |
nextLong(long origin, long bound) 返回指定原点(包含)与指定的边界(不包括)之间的伪随机值 long 。 |
SplittableRandom |
split() 构造并返回一个新的SplittableRandom实例,该实例与此实例不共享可变状态。 |
Public constructors
SplittableRandom
SplittableRandom (long seed)
使用指定的初始种子创建一个新的SplittableRandom实例。 在同一程序中使用相同种子创建的SplittableRandom实例会生成相同的值序列。
Parameters |
seed |
long : the initial seed |
SplittableRandom
SplittableRandom ()
创建一个新的SplittableRandom实例,该实例可能会生成统计独立于当前程序中任何其他实例的值的序列值; 并且可能并且通常确实会因程序调用而异。
Public methods
doubles
DoubleStream doubles (long streamSize)
返回产生所述给定流streamSize
数的伪随机的double
从该发电机和/或从它一个分割值; 每个值在零(包括)和一个(独占)之间。
Parameters |
streamSize |
long : the number of values to generate |
doubles
DoubleStream doubles ()
返回来自此生成器的伪随机数double
的有效无限流和/或从其中分离出的一个; 每个值在零(包括)和一个(独占)之间。
Implementation Note:
- This method is implemented to be equivalent to
doubles(Long.MAX_VALUE)
.
doubles
DoubleStream doubles (double randomNumberOrigin,
double randomNumberBound)
返回来自该生成器的伪随机数double
的有效无限流和/或从其中分离出的一个; 每个值符合给定的原点(包括)和绑定(独占)。
Implementation Note:
- This method is implemented to be equivalent to
doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)
.
Parameters |
randomNumberOrigin |
double : the origin (inclusive) of each random value |
randomNumberBound |
double : the bound (exclusive) of each random value |
Returns |
DoubleStream |
a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive) |
doubles
DoubleStream doubles (long streamSize,
double randomNumberOrigin,
double randomNumberBound)
返回产生所述给定流streamSize
数的伪随机的double
从该发电机和/或从它一个分割值; 每个值符合给定的原点(包括)和绑定(独占)。
Parameters |
streamSize |
long : the number of values to generate |
randomNumberOrigin |
double : the origin (inclusive) of each random value |
randomNumberBound |
double : the bound (exclusive) of each random value |
Returns |
DoubleStream |
a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive) |
ints
IntStream ints (long streamSize)
返回产生所述给定流 streamSize
数的伪随机的 int
从该发电机和/或从它一个分割值。
Parameters |
streamSize |
long : the number of values to generate |
Returns |
IntStream |
a stream of pseudorandom int values |
ints
IntStream ints (long streamSize,
int randomNumberOrigin,
int randomNumberBound)
返回产生所述给定流streamSize
数的伪随机的int
从该发电机和/或从它一个分割值; 每个值符合给定的原点(包括)和绑定(独占)。
Parameters |
streamSize |
long : the number of values to generate |
randomNumberOrigin |
int : the origin (inclusive) of each random value |
randomNumberBound |
int : the bound (exclusive) of each random value |
Returns |
IntStream |
a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive) |
Throws |
IllegalArgumentException |
if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound |
ints
IntStream ints (int randomNumberOrigin,
int randomNumberBound)
返回来自此生成器的伪随机数int
的有效无限流和/或从其中分离出的一个; 每个值符合给定的原点(包括)和绑定(独占)。
Implementation Note:
- This method is implemented to be equivalent to
ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)
.
Parameters |
randomNumberOrigin |
int : the origin (inclusive) of each random value |
randomNumberBound |
int : the bound (exclusive) of each random value |
Returns |
IntStream |
a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive) |
ints
IntStream ints ()
返回来自此生成器的伪随机数 int
的有效无限流和/或从其中分离出的一个。
Implementation Note:
- This method is implemented to be equivalent to
ints(Long.MAX_VALUE)
.
Returns |
IntStream |
a stream of pseudorandom int values |
longs
LongStream longs ()
返回伪随机的有效无限流 long
值从该发电机和/或从它一个分割。
Implementation Note:
- This method is implemented to be equivalent to
longs(Long.MAX_VALUE)
.
Returns |
LongStream |
a stream of pseudorandom long values |
longs
LongStream longs (long streamSize)
返回产生所述给定流 streamSize
数的伪随机的 long
从该发电机和/或从它一个分割值。
Parameters |
streamSize |
long : the number of values to generate |
Returns |
LongStream |
a stream of pseudorandom long values |
longs
LongStream longs (long randomNumberOrigin,
long randomNumberBound)
返回来自此生成器的伪随机数long
的有效无限流和/或从其中分离出的一个; 每个值符合给定的原点(包括)和绑定(独占)。
Implementation Note:
- This method is implemented to be equivalent to
longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)
.
Parameters |
randomNumberOrigin |
long : the origin (inclusive) of each random value |
randomNumberBound |
long : the bound (exclusive) of each random value |
Returns |
LongStream |
a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive) |
longs
LongStream longs (long streamSize,
long randomNumberOrigin,
long randomNumberBound)
返回产生所述给定流streamSize
数的伪随机的long
从该发电机和/或从它一个分割值; 每个值符合给定的原点(包括)和绑定(独占)。
Parameters |
streamSize |
long : the number of values to generate |
randomNumberOrigin |
long : the origin (inclusive) of each random value |
randomNumberBound |
long : the bound (exclusive) of each random value |
Returns |
LongStream |
a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive) |
Throws |
IllegalArgumentException |
if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound |
nextBoolean
boolean nextBoolean ()
返回伪随机值 boolean
值。
Returns |
boolean |
a pseudorandom boolean value |
nextDouble
double nextDouble ()
返回0(含)和1( double
之间的伪随机值 double
。
Returns |
double |
a pseudorandom double value between zero (inclusive) and one (exclusive) |
nextDouble
double nextDouble (double bound)
返回0.0(包含)和指定的边界(不包括)之间的伪随机值 double
。
Parameters |
bound |
double : the upper bound (exclusive). Must be positive. |
Returns |
double |
a pseudorandom double value between zero (inclusive) and the bound (exclusive) |
nextDouble
double nextDouble (double origin,
double bound)
返回指定原点(包含)和绑定(独占)之间的伪随机值 double
。
Parameters |
origin |
double : the least value returned |
bound |
double : the upper bound (exclusive) |
Returns |
double |
a pseudorandom double value between the origin (inclusive) and the bound (exclusive) |
nextInt
int nextInt ()
返回伪随机值 int
值。
Returns |
int |
a pseudorandom int value |
nextInt
int nextInt (int origin,
int bound)
返回一个伪随机 int
指定的原点(含)和指定约束(不含)之间的值。
Parameters |
origin |
int : the least value returned |
bound |
int : the upper bound (exclusive) |
Returns |
int |
a pseudorandom int value between the origin (inclusive) and the bound (exclusive) |
nextInt
int nextInt (int bound)
返回零(包括)和指定的边界(独占)之间的伪随机值 int
。
Parameters |
bound |
int : the upper bound (exclusive). Must be positive. |
Returns |
int |
a pseudorandom int value between zero (inclusive) and the bound (exclusive) |
nextLong
long nextLong (long bound)
返回零(包括)与指定的边界(独占)之间的伪随机值 long
。
Parameters |
bound |
long : the upper bound (exclusive). Must be positive. |
Returns |
long |
a pseudorandom long value between zero (inclusive) and the bound (exclusive) |
nextLong
long nextLong ()
返回伪随机值 long
值。
Returns |
long |
a pseudorandom long value |
nextLong
long nextLong (long origin,
long bound)
返回指定原点(包含)与指定的边界(不包含)之间的伪随机值 long
。
Parameters |
origin |
long : the least value returned |
bound |
long : the upper bound (exclusive) |
Returns |
long |
a pseudorandom long value between the origin (inclusive) and the bound (exclusive) |
split
SplittableRandom split ()
构造并返回一个新的SplittableRandom实例,该实例与此实例不共享可变状态。 然而,以极高的概率,由两个对象共同生成的值集具有相同的统计属性,就好像使用单个SplittableRandom对象由单个线程生成相同数量的值一样。 可以使用split()
方法将两个对象中的任一个或两个对象进一步分割,并且相同的预期统计特性适用于由这种递归分割构造的整个发生器组。