Most visited

Recently visited

Added in API level 21

ThreadLocalRandom

public class ThreadLocalRandom
extends Random

java.lang.Object
   ↳ java.util.Random
     ↳ java.util.concurrent.ThreadLocalRandom


随机数发生器与当前线程隔离。 Math类使用的全局Random生成器一样, ThreadLocalRandom也使用内部生成的种子进行初始化,否则可能不会进行修改。 如果适用,在并发程序中使用ThreadLocalRandom而不是共享的Random对象通常会遇到少得多的开销和争用。 当多个任务(例如,每个ForkJoinTask )在线程池中并行使用随机数时,使用ThreadLocalRandom尤其合适。

这个类的用途通常应该是这样的形式: ThreadLocalRandom.current().nextX(...) (其中XIntLong等)。 当所有用法都是这种形式时,不可能在多个线程中意外共享一个ThreadLocalRandom

该类还提供了其他常用的有界随机生成方法。

ThreadLocalRandom实例不具有密码安全性。 请考虑在安全敏感的应用程序中使用SecureRandom 此外,除非system property java.util.secureRandomSeed设置为true否则默认构造的实例不使用加密随机种子。

Summary

Public methods

static ThreadLocalRandom current()

返回当前线程的 ThreadLocalRandom

DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)

返回产生给定 streamSize数量的伪随机数 double值的流,每个值符合给定原点(包含)和绑定(排除)。

DoubleStream doubles(long streamSize)

返回产生所述给定流 streamSize数的伪随机的 double值,每个零(含)和一个(不包括)之间。

DoubleStream doubles()

返回一个有效无限的伪随机值 double值,每个值在零(含)和一(独占)之间。

DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)

返回一个有效无限数量的伪随机数 double值,每个值符合给定原点(包含)和绑定(独占)。

IntStream ints(long streamSize)

返回产生所述给定流 streamSize数的伪随机的 int值。

IntStream ints(int randomNumberOrigin, int randomNumberBound)

返回一个有效无限的伪随机值 int值,每个值符合给定原点(包含)和绑定(独占)。

IntStream ints()

返回伪随机值 int的有效无限流。

IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)

返回产生 streamSize伪随机数 int值的流,每个值都符合给定原点(包含)和绑定(排除)。

LongStream longs(long streamSize)

返回产生所述给定流 streamSize数的伪随机的 long值。

LongStream longs()

返回伪随机值 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

float nextFloat()

返回零(含)和一(独占)之间的伪随机值 float

double nextGaussian()

从该随机数生成器的序列返回下一个伪随机数,高斯(“正常”)分布 double值,平均值为 0.0 ,标准差为 1.0

int nextInt(int origin, int bound)

返回指定原点(包含)和指定的边界(不包括)之间的伪随机值 int

int nextInt()

返回伪随机值 int值。

int nextInt(int bound)

返回零(含)和指定的边界(独占)之间的伪随机值 int

long nextLong(long origin, long bound)

返回指定原点(包含)与指定的边界(不包含)之间的伪随机值 long

long nextLong(long bound)

返回零(包含)和指定的边界(独占)之间的伪随机值 long

long nextLong()

返回伪随机值 long值。

void setSeed(long seed)

抛出 UnsupportedOperationException

Protected methods

int next(int bits)

生成下一个伪随机数。

Inherited methods

From class java.util.Random
From class java.lang.Object

Public methods

current

Added in API level 21
ThreadLocalRandom current ()

返回当前线程的 ThreadLocalRandom

Returns
ThreadLocalRandom the current thread's ThreadLocalRandom

doubles

Added in API level 24
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)
Throws
IllegalArgumentException if streamSize is less than zero
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

doubles

Added in API level 24
DoubleStream doubles (long streamSize)

返回产生给定 streamSize个伪随机数 double数值的数据流,每个数值介于零(含)和一(独占)之间。

Parameters
streamSize long: the number of values to generate
Returns
DoubleStream a stream of double values
Throws
IllegalArgumentException if streamSize is less than zero

doubles

Added in API level 24
DoubleStream doubles ()

返回一个有效无限数量的伪随机数 double值,每个值都在零(含)和一(独占)之间。

Implementation Note:
  • This method is implemented to be equivalent to doubles(Long.MAX_VALUE).
Returns
DoubleStream a stream of pseudorandom double values

doubles

Added in API level 24
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)
Throws
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

ints

Added in API level 24
IntStream ints (long streamSize)

返回产生给定 streamSize个伪随机数 int值的流。

Parameters
streamSize long: the number of values to generate
Returns
IntStream a stream of pseudorandom int values
Throws
IllegalArgumentException if streamSize is less than zero

ints

Added in API level 24
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)
Throws
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

ints

Added in API level 24
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

ints

Added in API level 24
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

longs

Added in API level 24
LongStream longs (long streamSize)

返回产生所述给定流 streamSize数的伪随机的 long值。

Parameters
streamSize long: the number of values to generate
Returns
LongStream a stream of pseudorandom long values
Throws
IllegalArgumentException if streamSize is less than zero

longs

Added in API level 24
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

Added in API level 24
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)
Throws
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

longs

Added in API level 24
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

Added in API level 21
boolean nextBoolean ()

返回伪随机值 boolean值。

Returns
boolean a pseudorandom boolean value

nextDouble

Added in API level 21
double nextDouble ()

返回零(含)和一(独占)之间的伪随机值 double

Returns
double a pseudorandom double value between zero (inclusive) and one (exclusive)

nextDouble

Added in API level 21
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)
Throws
IllegalArgumentException if bound is not positive

nextDouble

Added in API level 21
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)
Throws
IllegalArgumentException if origin is greater than or equal to bound

nextFloat

Added in API level 21
float nextFloat ()

返回零(含)和一(独占)之间的伪随机值 float

Returns
float a pseudorandom float value between zero (inclusive) and one (exclusive)

nextGaussian

Added in API level 21
double nextGaussian ()

返回下一个伪随机数,高斯(“正常”)分布 double值,平均值为 0.0 ,标准偏差为 1.0来自此随机数生成器的序列。

nextGaussian的一般合同是从(大约)平均 0.0和标准偏差 1.0的通常正态分布中选择的一个 double值被伪随机地生成并返回。

方法 nextGaussian由类 Random实现,就像通过以下线程安全版本一样:

 private double nextNextGaussian;
 private boolean haveNextNextGaussian = false;

 public double nextGaussian() {
   if (haveNextNextGaussian) {
     haveNextNextGaussian = false;
     return nextNextGaussian;
   } else {
     double v1, v2, s;
     do {
       v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
       v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
       s = v1 * v1 + v2 * v2;
     } while (s >= 1 || s == 0);
     double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
     nextNextGaussian = v2 * multiplier;
     haveNextNextGaussian = true;
     return v1 * multiplier;
   }
 }
This uses the polar method of G. E. P. Box, M. E. Muller, and G. Marsaglia, as described by Donald E. Knuth in The Art of Computer Programming, Volume 3: Seminumerical Algorithms, section 3.4.1, subsection C, algorithm P. Note that it generates two independent values at the cost of only one call to StrictMath.log and one call to StrictMath.sqrt.

Returns
double the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence

nextInt

Added in API level 21
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)
Throws
IllegalArgumentException if origin is greater than or equal to bound

nextInt

Added in API level 21
int nextInt ()

返回伪随机值 int值。

Returns
int a pseudorandom int value

nextInt

Added in API level 21
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)
Throws
IllegalArgumentException if bound is not positive

nextLong

Added in API level 21
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)
Throws
IllegalArgumentException if origin is greater than or equal to bound

nextLong

Added in API level 21
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)
Throws
IllegalArgumentException if bound is not positive

nextLong

Added in API level 21
long nextLong ()

返回伪随机值 long值。

Returns
long a pseudorandom long value

setSeed

Added in API level 21
void setSeed (long seed)

抛出UnsupportedOperationException 不支持在此生成器中设置种子。

Parameters
seed long: the initial seed
Throws
UnsupportedOperationException always

Protected methods

next

Added in API level 21
int next (int bits)

生成下一个伪随机数。 子类应该覆盖这个,因为这被所有其他方法使用。

next的一般合约是它返回一个int值,如果参数bits介于132 (含)之间,那么返回值的许多低位将是(近似)独立选择的位值,其中每一个(大约)相同可能是01 方法next由类Random通过原子更新种子来实现

(seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)
and returning
(int)(seed >>> (48 - bits)).
This is a linear congruential pseudorandom number generator, as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 3: Seminumerical Algorithms, section 3.2.1.

Parameters
bits int: random bits
Returns
int the next pseudorandom value from this random number generator's sequence

Hooray!