java.util.Random
generator used by the java.lang.Math class,
a ThreadLocalRandom is initialized
with an internally generated seed that may not otherwise be
modified. When applicable, use of ThreadLocalRandom rather
than shared Random objects in concurrent programs will
typically encounter much less overhead and contention. Use of
ThreadLocalRandom is particularly appropriate when multiple
tasks (for example, each a ForkJoinTask) use random numbers
in parallel in thread pools.
Usages of this class should typically be of the form:
ThreadLocalRandom.current().nextX(...) (where
X is Int, Long, etc).
When all usages are of this form, it is never possible to
accidentally share a ThreadLocalRandom across multiple threads.
This class also provides additional commonly used bounded random generation methods.
Instances of ThreadLocalRandom are not cryptographically
secure. Consider instead using java.
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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final long | GOLDEN_GAMMA
The seed increment. |
| private static final long | |
| pack-priv boolean | initialized
Field used only during singleton initialization. |
| private static final ThreadLocalRandom | instance
The common ThreadLocalRandom |
| private static final JavaLangAccess | |
| private static final long | |
| private static final int | PROBE_INCREMENT
The increment for generating probe values. |
| private static final AtomicInteger | probeGenerator
Generates per-thread initialization/probe field |
| private static final long | |
| private static final long | |
| private static final AtomicLong | seeder
The next seed for default constructors. |
| private static final long | SEEDER_INCREMENT
The increment of seeder per new instance. |
| private static final ObjectStreamField[] | |
| private static final long | |
| private static final long | |
| private static final Unsafe |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv static final int | advanceProbe(int probe)
Pseudo-randomly advances and records the given probe value for the given thread. |
| public static ThreadLocalRandom | Returns: the current thread'sThreadLocalRandomReturns the current thread's |
| public DoubleStream | doubles(long streamSize)
Overrides java. Overrides default java. Returns a stream producing the given |
| public DoubleStream | doubles()
Overrides java. Overrides default java. Returns an effectively unlimited stream of pseudorandom |
| public DoubleStream | doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
Overrides java. Overrides default java. Returns a stream producing the given |
| public DoubleStream | doubles(double randomNumberOrigin, double randomNumberBound)
Overrides java. Overrides default java. Returns an effectively unlimited stream of pseudorandom |
| pack-priv static final void | |
| pack-priv static final int | |
| public IntStream | ints(long streamSize)
Overrides java. Overrides default java. Returns a stream producing the given |
| public IntStream | ints()
Overrides java. Overrides default java. Returns an effectively unlimited stream of pseudorandom |
| public IntStream | ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Overrides java. Overrides default java. Returns a stream producing the given |
| public IntStream | ints(int randomNumberOrigin, int randomNumberBound)
Overrides java. Overrides default java. Returns an effectively unlimited stream of pseudorandom |
| pack-priv static final void | |
| public LongStream | longs(long streamSize)
Overrides java. Overrides default java. Returns a stream producing the given |
| public LongStream | longs()
Overrides java. Overrides default java. Returns an effectively unlimited stream of pseudorandom |
| public LongStream | longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
Overrides java. Overrides default java. Returns a stream producing the given |
| public LongStream | longs(long randomNumberOrigin, long randomNumberBound)
Overrides java. Overrides default java. Returns an effectively unlimited stream of pseudorandom |
| private static int | |
| protected int | Returns: the next pseudorandom value from this random number generator's sequencerandom bits bits)Overrides java. Generates a pseudorandom number with the indicated number of low-order bits. |
| public boolean | nextBoolean()
Overrides java. Overrides default java. Returns the next pseudorandom, uniformly distributed
|
| public double | nextDouble()
Overrides java. Overrides default java. Returns the next pseudorandom, uniformly distributed
|
| public double | nextDouble(double bound)
Overrides default java. Returns a pseudorandomly chosen |
| public double | nextDouble(double origin, double bound)
Overrides default java. Returns a pseudorandomly chosen |
| public float | nextFloat()
Overrides java. Overrides default java. Returns the next pseudorandom, uniformly distributed |
| public float | nextFloat(float bound)
Overrides default java. Returns a pseudorandomly chosen |
| public float | nextFloat(float origin, float bound)
Overrides default java. Returns a pseudorandomly chosen |
| public double | Returns: adouble value pseudorandomly chosen from a
Gaussian distributionOverrides java. Overrides default java. Returns a |
| public int | nextInt()
Overrides java. Overrides default java. Returns the next pseudorandom, uniformly distributed |
| public int | nextInt(int bound)
Overrides java. Overrides default java. Returns a pseudorandom, uniformly distributed |
| public int | nextInt(int origin, int bound)
Overrides default java. Returns a pseudorandomly chosen |
| public long | nextLong()
Overrides java. Implements java. Returns the next pseudorandom, uniformly distributed |
| public long | nextLong(long bound)
Overrides default java. Returns a pseudorandomly chosen |
| public long | nextLong(long origin, long bound)
Overrides default java. Returns a pseudorandomly chosen |
| pack-priv static final int | |
| pack-priv final long | nextSeed()
Update the thread local seed value by adding to it the sum
of |
| private Object | |
| public void | |
| private void | writeObject(ObjectOutputStream
the stream s)Hides java. Saves the |
| GOLDEN_GAMMA | back to summary |
|---|---|
| private static final long GOLDEN_GAMMA The seed increment. This must be an odd value for the generator to have the maximum period (2 to the 64th power). The value 0x9e3779b97f4a7c15L is odd, and moreover consists of the first 64 bits of the fractional part of the golden ratio, which is known to generate good Weyl sequences. | |
| INHERITABLETHREADLOCALS | back to summary |
|---|---|
| private static final long INHERITABLETHREADLOCALS | |
| initialized | back to summary |
|---|---|
| pack-priv boolean initialized Field used only during singleton initialization. True when constructor completes. | |
| instance | back to summary |
|---|---|
| private static final ThreadLocalRandom instance The common ThreadLocalRandom | |
| JLA | back to summary |
|---|---|
| private static final JavaLangAccess JLA | |
| PROBE | back to summary |
|---|---|
| private static final long PROBE | |
| PROBE_INCREMENT | back to summary |
|---|---|
| private static final int PROBE_INCREMENT The increment for generating probe values. | |
| probeGenerator | back to summary |
|---|---|
| private static final AtomicInteger probeGenerator Generates per-thread initialization/probe field | |
| SECONDARY | back to summary |
|---|---|
| private static final long SECONDARY | |
| SEED | back to summary |
|---|---|
| private static final long SEED | |
| seeder | back to summary |
|---|---|
| private static final AtomicLong seeder The next seed for default constructors. | |
| SEEDER_INCREMENT | back to summary |
|---|---|
| private static final long SEEDER_INCREMENT The increment of seeder per new instance. | |
| serialPersistentFields | back to summary |
|---|---|
| private static final ObjectStreamField[] serialPersistentFields Hides java.
| |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| THREADLOCALS | back to summary |
|---|---|
| private static final long THREADLOCALS | |
| U | back to summary |
|---|---|
| private static final Unsafe U | |
| ThreadLocalRandom | back to summary |
|---|---|
| private ThreadLocalRandom() Constructor used only for static singleton | |
| advanceProbe | back to summary |
|---|---|
| pack-priv static final int advanceProbe(int probe) Pseudo-randomly advances and records the given probe value for the given thread. | |
| current | back to summary |
|---|---|
| public static ThreadLocalRandom current() Returns the current thread's
| |
| doubles | back to summary |
|---|---|
| public DoubleStream doubles(long streamSize) Overrides java. Overrides default java. Doc from java. Returns a stream producing the given A pseudorandom
| |
| doubles | back to summary |
|---|---|
| public DoubleStream doubles() Overrides java. Overrides default java. Doc from java. Returns an effectively unlimited stream of pseudorandom A pseudorandom Implementation Note This method is implemented to be equivalent to
| |
| doubles | back to summary |
|---|---|
| public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) Overrides java. Overrides default java. Doc from java. Returns a stream producing the given
| |
| doubles | back to summary |
|---|---|
| public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) Overrides java. Overrides default java. Doc from java. Returns an effectively unlimited stream of pseudorandom Implementation Note This method is implemented to be equivalent to
| |
| eraseThreadLocals | back to summary |
|---|---|
| pack-priv static final void eraseThreadLocals(Thread thread) Erases ThreadLocals by nulling out Thread maps. | |
| getProbe | back to summary |
|---|---|
| pack-priv static final int getProbe() Returns the probe value for the current thread without forcing initialization. Note that invoking ThreadLocalRandom.current() can be used to force initialization on zero return. | |
| ints | back to summary |
|---|---|
| public IntStream ints(long streamSize) Overrides java. Overrides default java. Doc from java. Returns a stream producing the given A pseudorandom
| |
| ints | back to summary |
|---|---|
| public IntStream ints() Overrides java. Overrides default java. Doc from java. Returns an effectively unlimited stream of pseudorandom A pseudorandom Implementation Note This method is implemented to be equivalent to
| |
| ints | back to summary |
|---|---|
| public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) Overrides java. Overrides default java. Doc from java. Returns a stream producing the given A pseudorandom
| |
| ints | back to summary |
|---|---|
| public IntStream ints(int randomNumberOrigin, int randomNumberBound) Overrides java. Overrides default java. Doc from java. Returns an effectively unlimited stream of pseudorandom A pseudorandom
Implementation Note This method is implemented to be equivalent to
| |
| localInit | back to summary |
|---|---|
| pack-priv static final void localInit() Initialize Thread fields for the current thread. Called only when Thread.threadLocalRandomProbe is zero, indicating that a thread local seed value needs to be generated. Note that even though the initialization is purely thread-local, we need to rely on (static) atomic generators to initialize the values. | |
| longs | back to summary |
|---|---|
| public LongStream longs(long streamSize) Overrides java. Overrides default java. Doc from java. Returns a stream producing the given A pseudorandom
| |
| longs | back to summary |
|---|---|
| public LongStream longs() Overrides java. Overrides default java. Doc from java. Returns an effectively unlimited stream of pseudorandom A pseudorandom Implementation Note This method is implemented to be equivalent to
| |
| longs | back to summary |
|---|---|
| public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) Overrides java. Overrides default java. Doc from java. Returns a stream producing the given A pseudorandom
| |
| longs | back to summary |
|---|---|
| public LongStream longs(long randomNumberOrigin, long randomNumberBound) Overrides java. Overrides default java. Doc from java. Returns an effectively unlimited stream of pseudorandom A pseudorandom
Implementation Note This method is implemented to be equivalent to
| |
| mix32 | back to summary |
|---|---|
| private static int mix32(long z) | |
| next | back to summary |
|---|---|
| protected int next(int bits) Overrides java. Generates a pseudorandom number with the indicated number of low-order bits. Because this class has no subclasses, this method cannot be invoked or overridden.
| |
| nextBoolean | back to summary |
|---|---|
| public boolean nextBoolean() Overrides java. Overrides default java. Doc from java. Returns the next pseudorandom, uniformly distributed
| |
| nextDouble | back to summary |
|---|---|
| public double nextDouble() Overrides java. Overrides default java. Doc from java. Returns the next pseudorandom, uniformly distributed
The general contract of
| |
| nextDouble | back to summary |
|---|---|
| public double nextDouble(double bound) Overrides default java. Doc from java. Returns a pseudorandomly chosen
| |
| nextDouble | back to summary |
|---|---|
| public double nextDouble(double origin, double bound) Overrides default java. Doc from java. Returns a pseudorandomly chosen
| |
| nextFloat | back to summary |
|---|---|
| public float nextFloat() Overrides java. Overrides default java. Doc from java. Returns the next pseudorandom, uniformly distributed The general contract of
| |
| nextFloat | back to summary |
|---|---|
| public float nextFloat(float bound) Overrides default java. Doc from java. Returns a pseudorandomly chosen
| |
| nextFloat | back to summary |
|---|---|
| public float nextFloat(float origin, float bound) Overrides default java. Doc from java. Returns a pseudorandomly chosen
| |
| nextGaussian | back to summary |
|---|---|
| public double nextGaussian() Overrides java. Overrides default java. Returns a Implementation Note This implementation invokes the default implementation of
| |
| nextInt | back to summary |
|---|---|
| public int nextInt() Overrides java. Overrides default java. Doc from java. Returns the next pseudorandom, uniformly distributed
| |
| nextInt | back to summary |
|---|---|
| public int nextInt(int bound) Overrides java. Overrides default java. Doc from java. Returns a pseudorandom, uniformly distributed
| |
| nextInt | back to summary |
|---|---|
| public int nextInt(int origin, int bound) Overrides default java. Doc from java. Returns a pseudorandomly chosen
| |
| nextLong | back to summary |
|---|---|
| public long nextLong() Overrides java. Implements java. Doc from java. Returns the next pseudorandom, uniformly distributed
| |
| nextLong | back to summary |
|---|---|
| public long nextLong(long bound) Overrides default java. Doc from java. Returns a pseudorandomly chosen
| |
| nextLong | back to summary |
|---|---|
| public long nextLong(long origin, long bound) Overrides default java. Doc from java. Returns a pseudorandomly chosen
| |
| nextSecondarySeed | back to summary |
|---|---|
| pack-priv static final int nextSecondarySeed() Returns the pseudo-randomly initialized or updated secondary seed. | |
| nextSeed | back to summary |
|---|---|
| pack-priv final long nextSeed() Update the thread local seed value by adding to it the sum
of | |
| readResolve | back to summary |
|---|---|
| private Object readResolve() Returns the | |
| setSeed | back to summary |
|---|---|
| public void setSeed(long seed) Overrides java. Throws
| |
| writeObject | back to summary |
|---|---|
| private void writeObject(ObjectOutputStream s) throws IOException Hides java. Saves the
| |
| Access | Constructor and Description |
|---|---|
| private |
| Access | back to summary |
|---|---|
| private Access() | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public double | nextDouble()
Overrides java. Overrides default java. Returns the next pseudorandom, uniformly distributed
|
| public int | nextInt()
Overrides java. Overrides default java. Returns the next pseudorandom, uniformly distributed |
| public long | nextLong()
Overrides java. Implements java. Returns the next pseudorandom, uniformly distributed |
| PROXY | back to summary |
|---|---|
| pack-priv static final Random PROXY | |
| ThreadLocalRandomProxy | back to summary |
|---|---|
| private ThreadLocalRandomProxy() | |
| nextDouble | back to summary |
|---|---|
| public double nextDouble() Overrides java. Overrides default java. Doc from java. Returns the next pseudorandom, uniformly distributed
The general contract of | |
| nextInt | back to summary |
|---|---|
| public int nextInt() Overrides java. Overrides default java. Doc from java. Returns the next pseudorandom, uniformly distributed | |
| nextLong | back to summary |
|---|---|
| public long nextLong() Overrides java. Implements java. Doc from java. Returns the next pseudorandom, uniformly distributed | |