- 已知的所有子接口:
-
RandomGenerator.ArbitrarilyJumpableGenerator,RandomGenerator.JumpableGenerator,RandomGenerator.LeapableGenerator,RandomGenerator.SplittableGenerator,RandomGenerator.StreamableGenerator
- 已知的所有实现类:
-
Random,SecureRandom,SplittableRandom,ThreadLocalRandom
RandomGenerator 接口旨在为生成随机或(更典型地)伪随机数(或布尔值)序列的对象提供一个通用协议。可以通过重复调用返回单个伪随机选择值的方法或调用返回伪随机选择值流的方法来获得这样的序列。
理想情况下,给定隐式或显式指定的值范围,每个值将独立且均匀地从该范围中选择。实际上,可能需要接受一些近似独立和均匀的结果。
对于 int、long 和 boolean 值,如果没有明确指定范围,则范围包括该类型的所有可能值。对于 float 和 double 值,首先总是从 0.0(包括)和 1.0(不包括)之间的 2w 个值集中均匀选择一个值,其中 w 是 Float.PRECISION(对于 float 值)和 Double.PRECISION(对于 double 值),使得相邻值之间的差异为 2−w(请注意,此集合是在 0.0(包括)和 1.0(不包括)之间的所有可表示浮点值集合的一个子集);然后,如果指定了显式范围,则所选数字将在计算上进行缩放和转换,以使其看起来大致均匀地从该显式范围中选择。
每个返回流的方法都会生成一系列值,每个值的选择方式与返回单个伪随机选择值的方法相似。例如,如果 r 实现了 RandomGenerator,那么方法调用 r.ints(100) 将返回一个包含 100 个 int 值的流。这些值不一定与如果 100 次调用 r.nextInt() 而返回的确切相同的值;只能保证流中的每个值都是以类似伪随机方式从相同范围中选择的。
通过使用伪随机算法实现 RandomGenerator 接口的每个对象都被假定包含有限数量的状态。使用这样的对象生成伪随机选择值会通过计算新状态作为当前状态的函数而改变其状态,而不参考除当前状态之外的任何信息。这样的对象的可能状态数称为其 周期。(某些实现 RandomGenerator 接口的对象可能是真正随机的,而不是伪随机的,例如依赖物理对象的统计行为来导出选择的值。这样的实现没有固定的周期。)
通常情况下,实现 RandomGenerator 接口的对象不需要是线程安全的。建议多线程应用程序使用 ThreadLocalRandom 或(最好是)实现 RandomGenerator.SplittableGenerator 或 RandomGenerator.JumpableGenerator 接口的伪随机数生成器。
实现 RandomGenerator 的对象通常不具有密码学安全性。相反,考虑使用 SecureRandom 来获取密码学安全的伪随机数生成器,供安全敏感的应用程序使用。但请注意,SecureRandom 确实实现了 RandomGenerator 接口,因此 SecureRandom 的实例可以在不需要安全生成器的应用程序中与其他类型的伪随机生成器互换使用。
除非另有明确说明,对于任何方法参数使用 null 都将导致 NullPointerException。
- 自 JDK 17 起:
- 17
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface该接口旨在为生成伪随机值序列的对象提供一个通用协议,并且可以轻松地向前“跳转”到状态周期中的远处位置。static interface该接口旨在为生成伪随机值的对象提供一个通用协议,并且可以轻松地向前“跳转”到状态周期中的适度位置(例如 264)。static interface该接口旨在为生成伪随机值序列的对象提供一个通用协议,并且不仅可以向前“跳转”,还可以向前“跳跃”到状态周期中的大位置(例如 2128)。static interface该接口旨在为生成伪随机值序列的对象提供一个通用协议,并且可以被“分割”为两个对象(原始对象和新对象),每个对象都遵循相同的协议(因此可以无限递归地分割)。static interface -
Method Summary
Modifier and TypeMethodDescriptiondefault DoubleStreamdoubles()返回一个有效无限的伪随机选择的double值流。default DoubleStreamdoubles(double randomNumberOrigin, double randomNumberBound) 返回一个有效无限的伪随机选择的double值流,其中每个值介于指定的起始值(包括)和指定的边界值(不包括)之间。default DoubleStreamdoubles(long streamSize) 返回生成给定streamSize数量的伪随机选择的double值流。default DoubleStreamdoubles(long streamSize, double randomNumberOrigin, double randomNumberBound) 返回生成给定streamSize数量的伪随机选择的double值流,其中每个值介于指定的起始值(包括)和指定的边界值(不包括)之间。static RandomGenerator返回一个满足具有大于或等于 64 位状态位的算法的RandomGenerator。default IntStreamints()返回一个有效无限的伪随机选择的int值流。default IntStreamints(int randomNumberOrigin, int randomNumberBound) 返回一个有效无限的伪随机选择的int值流,其中每个值介于指定的起始值(包括)和指定的边界值(不包括)之间。default IntStreamints(long streamSize) 返回生成给定streamSize数量的伪随机选择的int值流。default IntStreamints(long streamSize, int randomNumberOrigin, int randomNumberBound) 返回生成给定streamSize数量的伪随机选择的int值流,其中每个值介于指定的起始值(包括)和指定的边界值(不包括)之间。default boolean如果 RandomGenerator(算法)的实现已被标记为弃用,则返回 true。default LongStreamlongs()返回一个有效无限的伪随机选择的long值流。default LongStreamlongs(long streamSize) 返回生成给定streamSize数量的伪随机选择的long值流。default LongStreamlongs(long randomNumberOrigin, long randomNumberBound) 返回一个有效无限的伪随机选择的long值流,其中每个值介于指定的起始值(包括)和指定的边界值(不包括)之间。default LongStreamlongs(long streamSize, long randomNumberOrigin, long randomNumberBound) 返回生成给定streamSize数量的伪随机选择的long值流,其中每个值介于指定的起始值(包括)和指定的边界值(不包括)之间。default boolean返回一个伪随机选择的boolean值。default voidnextBytes(byte[] bytes) 使用从值范围在 -128(包括)和 127(包括)之间均匀选择的生成字节值填充用户提供的字节数组。default double返回介于零(包括)和一(不包括)之间的伪随机double值。default doublenextDouble(double bound) 返回介于零(包括)和指定的边界值(不包括)之间的伪随机选择的double值。default doublenextDouble(double origin, double bound) 返回介于指定的起始值(包括)和指定的边界值(不包括)之间的伪随机选择的double值。default double返回从均值为 1 的指数分布中伪随机选择的非负double值。default float返回介于零(包括)和一(不包括)之间的伪随机float值。default floatnextFloat(float bound) 返回介于零(包括)和指定的边界值(不包括)之间的伪随机选择的float值。default floatnextFloat(float origin, float bound) 返回介于指定的起始值(包括)和指定的边界值(不包括)之间的伪随机选择的float值。default double从均值为0,标准差为1的高斯(正态)分布中伪随机选择一个double值。default doublenextGaussian(double mean, double stddev) 从具有由参数指定的均值和标准差的高斯(正态)分布中伪随机选择一个double值。default intnextInt()返回一个伪随机选择的int值。default intnextInt(int bound) 返回一个介于零(包括)和指定边界(不包括)之间的伪随机选择的int值。default intnextInt(int origin, int bound) 返回一个介于指定起始值(包括)和指定边界(不包括)之间的伪随机选择的int值。longnextLong()返回一个伪随机选择的long值。default longnextLong(long bound) 返回一个介于零(包括)和指定边界(不包括)之间的伪随机选择的long值。default longnextLong(long origin, long bound) 返回一个介于指定起始值(包括)和指定边界(不包括)之间的伪随机选择的long值。static RandomGenerator
-
Method Details
-
of
- 参数:
-
name- 随机数生成器的名称算法 - 返回:
-
一个
RandomGenerator实例 - 抛出:
-
NullPointerException- 如果名称为null -
IllegalArgumentException- 如果找不到指定的算法
-
getDefault
返回一个符合具有状态位大于或等于64的RandomGenerator的最低要求的实例。- 实现要求:
-
由于算法会随时间改进,因此不能保证此方法随时间返回相同的算法。
默认实现选择L32X64MixRandom。
- 返回:
-
一个
RandomGenerator
-
isDeprecated
default boolean isDeprecated()如果RandomGenerator(算法)的实现已标记为过时,则返回true。- 实现要求:
- 默认实现检查@Deprecated注解。
- 实现说明:
- 随机数生成器算法会随时间演变;新算法将被引入,旧算法将失去地位。如果认为旧算法不适合继续使用,它将被标记为过时,以指示将来可能会删除它。
- 返回:
- 如果RandomGenerator(算法)的实现已标记为过时,则返回true
-
doubles
返回一个有效无限的伪随机选择的double值流。- 实现要求:
-
默认实现生成一个顺序流,重复调用
nextDouble()。 - 实现说明:
-
可以以与
doubles(Long.MAX_VALUE)等效的方式实现此方法。 - 返回:
-
一个有效无限的伪随机选择的
double值流
-
doubles
返回一个有效无限的伪随机选择的double值流,其中每个值介于指定起始值(包括)和指定边界(不包括)之间。- 实现要求:
-
默认实现生成一个顺序流,重复调用
nextDouble(randomNumberOrigin, randomNumberBound)。 - 实现说明:
-
可以以与
doubles(Long.MAX_VALUE,randomNumberOrigin, randomNumberBound)等效的方式实现此方法。 - 参数:
-
randomNumberOrigin- 可产生的最小值 -
randomNumberBound- 每个生成值的上限(不包括) - 返回:
-
一个伪随机选择的
double值流,每个值介于指定起始值(包括)和指定边界(不包括)之间 - 抛出:
-
IllegalArgumentException- 如果randomNumberOrigin不是有限的,或者randomNumberBound不是有限的,或者randomNumberOrigin大于或等于randomNumberBound
-
doubles
返回一个生成给定streamSize数量的伪随机选择的double值的流。- 实现要求:
-
默认实现生成一个顺序流,重复调用
nextDouble()。 - 参数:
-
streamSize- 要生成的值的数量 - 返回:
-
一个伪随机选择的
double值流 - 抛出:
-
IllegalArgumentException- 如果streamSize小于零
-
doubles
返回一个生成给定streamSize数量的伪随机选择的double值的流,其中每个值介于指定起始值(包括)和指定边界(不包括)之间。- Implementation Requirements:
-
The default implementation produces a sequential stream that repeatedly calls
nextDouble(randomNumberOrigin, randomNumberBound). - Parameters:
-
streamSize- the number of values to generate -
randomNumberOrigin- the least value that can be produced -
randomNumberBound- the upper bound (exclusive) for each value produced - Returns:
-
a stream of pseudorandomly chosen
doublevalues, each between the specified origin (inclusive) and the specified bound (exclusive) - Throws:
-
IllegalArgumentException- ifstreamSizeis less than zero, orrandomNumberOriginis not finite, orrandomNumberBoundis not finite, orrandomNumberOriginis greater than or equal torandomNumberBound
-
ints
Returns an effectively unlimited stream of pseudorandomly chosenintvalues.- Implementation Requirements:
-
The default implementation produces a sequential stream that repeatedly calls
nextInt(). - Implementation Note:
-
It is permitted to implement this method in a manner equivalent to
ints(Long.MAX_VALUE). - Returns:
-
a stream of pseudorandomly chosen
intvalues
-
ints
Returns an effectively unlimited stream of pseudorandomly chosenintvalues, where each value is between the specified origin (inclusive) and the specified bound (exclusive).- Implementation Requirements:
-
The default implementation produces a sequential stream that repeatedly calls
nextInt(randomNumberOrigin, randomNumberBound). - Implementation Note:
-
It is permitted to implement this method in a manner equivalent to
ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
-
randomNumberOrigin- the least value that can be produced -
randomNumberBound- the upper bound (exclusive) for each value produced - Returns:
-
a stream of pseudorandomly chosen
intvalues, each between the specified origin (inclusive) and the specified bound (exclusive) - Throws:
-
IllegalArgumentException- ifrandomNumberOriginis greater than or equal torandomNumberBound
-
ints
Returns a stream producing the givenstreamSizenumber of pseudorandomly chosenintvalues.- Implementation Requirements:
-
The default implementation produces a sequential stream that repeatedly calls
nextInt(). - Parameters:
-
streamSize- the number of values to generate - Returns:
-
a stream of pseudorandomly chosen
intvalues - Throws:
-
IllegalArgumentException- ifstreamSizeis less than zero
-
ints
Returns a stream producing the givenstreamSizenumber of pseudorandomly chosenintvalues, where each value is between the specified origin (inclusive) and the specified bound (exclusive).- 实现要求:
-
默认实现生成一个顺序流,重复调用
nextInt(randomNumberOrigin, randomNumberBound)。 - 参数:
-
streamSize- 要生成的值的数量 -
randomNumberOrigin- 可产生的最小值 -
randomNumberBound- 每个生成值的上限(不包括) - 返回:
-
一个伪随机选择的
int值流,每个值介于指定的起始值(包括)和指定的上限值(不包括)之间 - 抛出:
-
IllegalArgumentException- 如果streamSize小于零,或randomNumberOrigin大于或等于randomNumberBound
-
longs
返回一个有效无限的伪随机选择的long值流。- 实现要求:
-
默认实现生成一个顺序流,重复调用
nextLong()。 - 实现说明:
-
允许以与
longs(Long.MAX_VALUE)等效的方式实现此方法。 - 返回:
-
一个伪随机选择的
long值流
-
longs
返回一个有效无限的伪随机选择的long值流,其中每个值介于指定的起始值(包括)和指定的上限值(不包括)之间。- 实现要求:
-
默认实现生成一个顺序流,重复调用
nextLong(randomNumberOrigin, randomNumberBound)。 - 实现说明:
-
允许以与
longs(Long.MAX_VALUE,randomNumberOrigin, randomNumberBound)等效的方式实现此方法。 - 参数:
-
randomNumberOrigin- 可产生的最小值 -
randomNumberBound- 每个生成值的上限(不包括) - 返回:
-
一个伪随机选择的
long值流,每个值介于指定的起始值(包括)和指定的上限值(不包括)之间 - 抛出:
-
IllegalArgumentException- 如果randomNumberOrigin大于或等于randomNumberBound
-
longs
返回一个生成给定streamSize数量的伪随机选择的long值流。- 实现要求:
-
默认实现生成一个顺序流,重复调用
nextLong()。 - 参数:
-
streamSize- 要生成的值的数量 - 返回:
-
一个伪随机选择的
long值流 - 抛出:
-
IllegalArgumentException- 如果streamSize小于零
-
longs
返回一个生成给定streamSize数量的伪随机选择的long值流,其中每个值介于指定的起始值(包括)和指定的上限值(不包括)之间。- 实现要求:
-
默认实现生成一个顺序流,重复调用
nextLong(randomNumberOrigin, randomNumberBound)。 - 参数:
-
streamSize- 要生成的值的数量 -
randomNumberOrigin- 可产生的最小值 -
randomNumberBound- 每个生成值的上限(不包括) - 返回:
-
一个伪随机选择的
long值流,每个值介于指定的起始值(包括)和指定的上限值(不包括)之间 - 抛出:
-
IllegalArgumentException- 如果streamSize小于零,或randomNumberOrigin大于或等于randomNumberBound
-
nextBoolean
default boolean nextBoolean()返回一个伪随机选择的boolean值。默认实现测试由
nextInt()生成的值的高阶位(符号位),因为一些伪随机数生成算法生成的值的高阶位在统计质量上优于低阶位。- 实现要求:
-
默认实现基于由
nextInt()生成的数字的符号位产生结果。 - 返回:
-
一个伪随机选择的
boolean值
-
nextBytes
default void nextBytes(byte[] bytes) 使用从-128(包括)到127(包括)之间的值范围均匀伪随机选择的值填充用户提供的字节数组。- 实现要求:
-
默认实现通过重复调用
nextLong()来生成结果。 - 实现说明:
-
用于填充字节数组的算法;
void nextBytes(byte[] bytes) { int i = 0; int len = bytes.length; for (int words = len >> 3; words--> 0; ) { long rnd = nextLong(); for (int n = 8; n--> 0; rnd >>>= Byte.SIZE) bytes[i++] = (byte)rnd; } if (i < len) for (long rnd = nextLong(); i < len; rnd >>>= Byte.SIZE) bytes[i++] = (byte)rnd; } - 参数:
-
bytes- 要填充伪随机字节的字节数组 - 抛出:
-
NullPointerException- 如果字节数组为null
-
nextFloat
default float nextFloat()返回一个介于零(包括)和一(不包括)之间的伪随机float值。- 实现要求:
-
默认实现使用从调用
nextInt()获取的Float.PRECISION高阶位。 - 返回:
-
一个介于零(包括)和一(不包括)之间的伪随机
float值
-
nextFloat
default float nextFloat(float bound) 返回一个介于零(包括)和指定上限(不包括)之间的伪随机选择的float值。- 实现要求:
-
默认实现检查
bound是否为正有限浮点数。然后调用nextFloat(),缩放结果以使最终结果介于0.0f(包括)和bound(不包括)之间。 - 参数:
-
bound- 返回值的上限(不包括)。必须为正且有限 - 返回:
-
一个介于零(包括)和上限(不包括)之间的伪随机选择的
float值 - 抛出:
-
IllegalArgumentException- 如果bound既不是正数也不是有限数
-
nextFloat
default float nextFloat(float origin, float bound) 返回一个介于指定起始值(包括)和指定上限值(不包括)之间的伪随机选择的float值。- 实现要求:
-
默认实现验证
origin和bound是否有效,然后调用nextFloat(),缩放和平移结果以适应origin和bound(不包括)之间。 - 参数:
-
origin- 可返回的最小值 -
bound- 上限(不包括) - 返回:
-
一个介于起始值(包括)和上限值(不包括)之间的伪随机选择的
float值 - 抛出:
-
IllegalArgumentException- 如果origin不是有限数,或bound不是有限数,或origin大于或等于bound
-
nextDouble
default double nextDouble()返回一个介于零(包括)和一(不包括)之间的伪随机double值。- 实现要求:
-
默认实现使用从调用
nextLong()获取的Double.PRECISION高阶位。 - 返回:
-
一个介于零(包括)和一(不包括)之间的伪随机
double值
-
nextDouble
default double nextDouble(double bound) 返回一个介于零(包括)和指定上限(不包括)之间的伪随机选择的double值。- 实现要求:
-
默认实现检查
bound是否为正有限双精度数。然后调用nextDouble(),缩放结果以使最终结果介于0.0(包括)和bound(不包括)之间。 - 参数:
-
bound- 返回值的上限(不包括)。必须为正且有限 - 返回:
-
一个介于零(包括)和上限(不包括)之间的伪随机选择的
double值 - 抛出:
-
IllegalArgumentException- 如果bound既不是正数也不是有限数
-
nextDouble
default double nextDouble(double origin, double bound) 返回一个介于指定起始值(包括)和指定上限值(不包括)之间的伪随机选择的double值。- 实现要求:
-
默认实现验证
origin和bound是否有效,然后调用nextDouble()对结果进行缩放和平移,使其适合于origin和bound之间(不包括bound)。 - 参数:
-
origin- 可返回的最小值 -
bound- 返回值的上限(不包括) - 返回:
-
在
origin(包括)和bound(不包括)之间选择的伪随机double值 - 抛出:
-
IllegalArgumentException- 如果origin不是有限的,或者bound不是有限的,或者origin大于或等于bound
-
nextInt
default int nextInt()返回一个伪随机选择的int值。- 实现要求:
-
默认实现使用从调用
nextLong()中获取的32个高阶位。 - 返回:
-
一个伪随机选择的
int值
-
nextInt
default int nextInt(int bound) 返回一个在零(包括)和指定上限(不包括)之间选择的伪随机int值。- 实现要求:
-
默认实现检查
bound是否为正整数int。然后调用nextInt(),限制结果大于或等于零且小于bound。如果bound是2的幂,则限制是一个简单的掩码操作。否则,通过调用nextInt()重新计算结果,直到结果大于或等于零且小于bound。 - 参数:
-
bound- 返回值的上限(不包括)。必须为正数。 - 返回:
-
在零(包括)和上限(不包括)之间选择的伪随机
int值 - 抛出:
-
IllegalArgumentException- 如果bound不是正数
-
nextInt
default int nextInt(int origin, int bound) 返回一个在指定原点(包括)和指定上限(不包括)之间选择的伪随机int值。- 实现要求:
-
默认实现检查
origin和bound是否为正整数int。然后调用nextInt(),限制结果大于或等于origin且小于bound。如果bound是2的幂,则限制是一个简单的掩码操作。否则,通过调用nextInt()重新计算结果,直到结果大于或等于origin且小于bound。 - 参数:
-
origin- 可返回的最小值 -
bound- 返回值的上限(不包括) - 返回:
-
在原点(包括)和上限(不包括)之间选择的伪随机
int值 - 抛出:
-
IllegalArgumentException- 如果origin大于或等于bound
-
nextLong
long nextLong()返回一个伪随机选择的long值。- 返回:
-
一个伪随机选择的
long值
-
nextLong
default long nextLong(long bound) 返回一个在零(包括)和指定上限(不包括)之间选择的伪随机long值。- 实现要求:
-
默认实现检查
bound是否为正整数long。然后调用nextLong(),限制结果大于或等于零且小于bound。如果bound是2的幂,则限制是一个简单的掩码操作。否则,通过调用nextLong()重新计算结果,直到结果大于或等于零且小于bound。 - 参数:
-
bound- 返回值的上限(不包括)。必须为正数。 - 返回:
-
在零(包括)和上限(不包括)之间选择的伪随机
long值 - 抛出:
-
IllegalArgumentException- 如果bound不是正数
-
nextLong
default long nextLong(long origin, long bound) 返回一个在指定原点(包括)和指定上限(不包括)之间选择的伪随机long值。- 实现要求:
-
默认实现检查
origin和bound是否为正整数long。然后调用nextLong(),限制结果大于或等于origin且小于bound。如果bound是2的幂,则限制是一个简单的掩码操作。否则,通过调用nextLong()重新计算结果,直到结果大于或等于origin且小于bound。 - 参数:
-
origin- 可返回的最小值 -
bound- 返回值的上限(不包括) - 返回:
-
在原点(包括)和上限(不包括)之间选择的伪随机
long值 - 抛出:
-
IllegalArgumentException- 如果origin大于或等于bound
-
nextGaussian
default double nextGaussian()返回一个从均值为0,标准差为1的高斯(正态)分布中伪随机选择的double值。- 实现要求:
- 默认实现使用McFarland的快速修改的ziggurat算法(主要是基于表的,偶尔通过计算和拒绝采样处理罕见情况)。Walker的别名方法用于从离散分布中抽样也起到作用。
- 返回:
-
从高斯分布中伪随机选择的
double值
-
nextGaussian
default double nextGaussian(double mean, double stddev) 返回一个从具有由参数指定的均值和标准差的高斯(正态)分布中伪随机选择的double值。- 实现要求:
- 默认实现使用McFarland的快速修改的ziggurat算法(主要是基于表的,偶尔通过计算和拒绝采样处理罕见情况)。Walker的别名方法用于从离散分布中抽样也起到作用。
- 参数:
-
mean- 要从中抽取的高斯分布的均值 -
stddev- 要从中抽取的高斯分布的标准差(方差的平方根) - 返回:
-
从指定高斯分布中伪随机选择的
double值 - 抛出:
-
IllegalArgumentException- 如果stddev为负数
-
nextExponential
default double nextExponential()返回一个非负的从均值为1的指数分布中伪随机选择的double值。- 实现要求:
- 默认实现使用McFarland的快速修改的ziggurat算法(主要是基于表的,偶尔通过计算和拒绝采样处理罕见情况)。Walker的别名方法用于从离散分布中抽样也起到作用。
- 返回:
-
从指数分布中伪随机选择的非负
double值
-