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 | |
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 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'sThreadLocalRandom Returns the current thread's |
public DoubleStream | doubles(long
the number of values to generate streamSize)Overrides java. Overrides default java. streamSize number of
pseudorandom double values, each between zero
(inclusive) and one (exclusive).
|
public DoubleStream | doubles()
Overrides java. Overrides default java. double values, each between zero (inclusive) and one
(exclusive).
|
public DoubleStream | doubles(long
the number of values to generate streamSize, double the origin (inclusive) of each random value randomNumberOrigin, double the bound (exclusive) of each random value randomNumberBound)Overrides java. Overrides default java. streamSize number of
pseudorandom double values, each conforming to the given origin
(inclusive) and bound (exclusive).
|
public DoubleStream | doubles(double
the origin (inclusive) of each random value randomNumberOrigin, double the bound (exclusive) of each random value randomNumberBound)Overrides java. Overrides default java. double values, each conforming to the given origin (inclusive) and bound
(exclusive).
|
pack-priv static final void | |
pack-priv static final int | |
public IntStream | ints(long
the number of values to generate streamSize)Overrides java. Overrides default java. streamSize number of
pseudorandom int values.
|
public IntStream | ints()
Overrides java. Overrides default java. int
values.
|
public IntStream | ints(long
the number of values to generate streamSize, int the origin (inclusive) of each random value randomNumberOrigin, int the bound (exclusive) of each random value randomNumberBound)Overrides java. Overrides default java. streamSize number
of pseudorandom int values, each conforming to the given
origin (inclusive) and bound (exclusive).
|
public IntStream | ints(int
the origin (inclusive) of each random value randomNumberOrigin, int the bound (exclusive) of each random value randomNumberBound)Overrides java. Overrides default java. int values, each conforming to the given origin (inclusive) and bound
(exclusive).
|
pack-priv static final void | |
public LongStream | longs(long
the number of values to generate streamSize)Overrides java. Overrides default java. streamSize number of
pseudorandom long values.
|
public LongStream | longs()
Overrides java. Overrides default java. long
values.
|
public LongStream | longs(long
the number of values to generate streamSize, long the origin (inclusive) of each random value randomNumberOrigin, long the bound (exclusive) of each random value randomNumberBound)Overrides java. Overrides default java. streamSize number of
pseudorandom long , each conforming to the given origin
(inclusive) and bound (exclusive).
|
public LongStream | longs(long
the origin (inclusive) of each random value randomNumberOrigin, long the bound (exclusive) of each random value randomNumberBound)Overrides java. Overrides default java. long values, each conforming to the given origin (inclusive) and bound
(exclusive).
|
private static int | |
protected int | Returns: the next pseudorandom value from this random number generator's sequencerandom bits bits)Overrides java. |
public boolean | nextBoolean()
Overrides java. Overrides default java. boolean value from this random number generator's
sequence.
|
public double | nextDouble()
Overrides java. Overrides default java. double value between 0.0 and
1.0 from this random number generator's sequence.
|
public double | nextDouble(double
the upper bound (exclusive) for the returned value.
Must be positive and finite bound)Overrides default java. double value between zero
(inclusive) and the specified bound (exclusive).
|
public double | nextDouble(double
the least value that can be returned origin, double the upper bound (exclusive) for the returned value bound)Overrides default java. double value between the
specified origin (inclusive) and the specified bound (exclusive).
|
public float | nextFloat()
Overrides java. Overrides default java. float
value between 0.0 and 1.0 from this random
number generator's sequence.
|
public float | nextFloat(float
the upper bound (exclusive) for the returned value.
Must be positive and finite bound)Overrides default java. float value between zero
(inclusive) and the specified bound (exclusive).
|
public float | nextFloat(float
the least value that can be returned origin, float the upper bound (exclusive) bound)Overrides default java. float value between the
specified origin (inclusive) and the specified bound (exclusive).
|
public int | nextInt()
Overrides java. Overrides default java. int
value from this random number generator's sequence.
|
public int | nextInt(int
the upper bound (exclusive). Must be positive. bound)Overrides java. Overrides default java. int value
between 0 (inclusive) and the specified value (exclusive), drawn from
this random number generator's sequence.
|
public int | nextInt(int
the least value that can be returned origin, int the upper bound (exclusive) for the returned value bound)Overrides default java. int value between the specified
origin (inclusive) and the specified bound (exclusive).
|
public long | nextLong()
Overrides java. Implements java. long
value from this random number generator's sequence.
|
public long | nextLong(long
the upper bound (exclusive) for the returned value.
Must be positive. bound)Overrides default java. long value between zero
(inclusive) and the specified bound (exclusive).
|
public long | nextLong(long
the least value that can be returned origin, long the upper bound (exclusive) for the returned value bound)Overrides default java. long value between the
specified origin (inclusive) and the specified bound (exclusive).
|
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 | |
pack-priv static final void | setInheritedAccessControlContext(Thread thread, AccessControlContext acc)
Deprecated
as a consequence of
AccessControlContext being deprecated.
|
public void | setSeed(long
the seed value seed)Overrides java. UnsupportedOperationException .
|
private void | writeObject(ObjectOutputStream
the stream s)Hides java. ThreadLocalRandom to a stream (that is, serializes it).
|
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 |
INHERITEDACCESSCONTROLCONTEXT | back to summary |
---|---|
private static final long INHERITEDACCESSCONTROLCONTEXT |
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 |
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 Implementation Note The default implementation checks that
|
nextDouble | back to summary |
---|---|
public double nextDouble(double origin, double bound) Overrides default java. Doc from java. Returns a pseudorandomly chosen Implementation Note The default implementation verifies that the
|
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 Implementation Note The default implementation checks that
|
nextFloat | back to summary |
---|---|
public float nextFloat(float origin, float bound) Overrides default java. Doc from java. Returns a pseudorandomly chosen Implementation Note The default implementation verifies that the
|
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 |
setInheritedAccessControlContext | back to summary |
---|---|
pack-priv static final void setInheritedAccessControlContext(Thread thread, AccessControlContext acc)
Deprecated as a consequence of |
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. double value between 0.0 and
1.0 from this random number generator's sequence.
|
public int | nextInt()
Overrides java. Overrides default java. int
value from this random number generator's sequence.
|
public long | nextLong()
Overrides java. Implements java. long
value from this random number generator's sequence.
|
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
|