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
accidently share a ThreadLocalRandom across multiple threads.
This class also provides additional commonly used bounded random generation methods.
| Modifier and Type | Field and Description |
|---|---|
| private static final long | |
| pack-priv boolean | initialized
Initialization flag to permit calls to setSeed to succeed only while executing the Random constructor. |
| private static final ThreadLocal | localRandom
The actual ThreadLocal |
| private static final long | mask
Hides java. |
| private static final long | |
| private long | |
| private long | |
| private long | |
| private long | |
| private long | |
| private long | |
| private long | |
| private long | |
| private long | rnd
The random seed. |
| private static final long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public static ThreadLocalRandom | Returns: the current thread'sThreadLocalRandomReturns the current thread's |
| protected int | |
| public double | Returns: the next valuethe bound on the random number to be returned. Must be
positive. n)Overrides default java. double value
between 0 (inclusive) and the specified value (exclusive).
|
| public double | Returns: the next valuethe least value returned least, double the upper bound (exclusive) bound)Overrides default java. |
| public int | Returns: the next valuethe least value returned least, int the upper bound (exclusive) bound)Overrides default java. |
| public long | Returns: the next valuethe bound on the random number to be returned. Must be
positive. n)Overrides default java. |
| public long | Returns: the next valuethe least value returned least, long the upper bound (exclusive) bound)Overrides default java. |
| public void | setSeed(long
the seed value seed)Overrides java. UnsupportedOperationException.
|
| addend | back to summary |
|---|---|
| private static final long addend Hides java. | |
| initialized | back to summary |
|---|---|
| pack-priv boolean initialized Initialization flag to permit calls to setSeed to succeed only while executing the Random constructor. We can't allow others since it would cause setting seed in one part of a program to unintentionally impact other usages by the thread. | |
| localRandom | back to summary |
|---|---|
| private static final ThreadLocal<ThreadLocalRandom> localRandom The actual ThreadLocal | |
| mask | back to summary |
|---|---|
| private static final long mask Hides java. | |
| multiplier | back to summary |
|---|---|
| private static final long multiplier Hides java. | |
| pad0 | back to summary |
|---|---|
| private long pad0 | |
| pad1 | back to summary |
|---|---|
| private long pad1 | |
| pad2 | back to summary |
|---|---|
| private long pad2 | |
| pad3 | back to summary |
|---|---|
| private long pad3 | |
| pad4 | back to summary |
|---|---|
| private long pad4 | |
| pad5 | back to summary |
|---|---|
| private long pad5 | |
| pad6 | back to summary |
|---|---|
| private long pad6 | |
| pad7 | back to summary |
|---|---|
| private long pad7 | |
| rnd | back to summary |
|---|---|
| private long rnd The random seed. We can't use super.seed. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| ThreadLocalRandom | back to summary |
|---|---|
| pack-priv ThreadLocalRandom() Constructor called only by localRandom.initialValue. | |
| current | back to summary |
|---|---|
| public static ThreadLocalRandom current() Returns the current thread's
| |
| next | back to summary |
|---|---|
| protected int next(int bits) Overrides java. Doc from java. Generates the next pseudorandom number. This method returns an
| |
| nextDouble | back to summary |
|---|---|
| public double nextDouble(double n) Overrides default java. Returns a pseudorandom, uniformly distributed
| |
| nextDouble | back to summary |
|---|---|
| public double nextDouble(double least, double bound) Overrides default java. Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
| |
| nextInt | back to summary |
|---|---|
| public int nextInt(int least, int bound) Overrides default java. Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
| |
| nextLong | back to summary |
|---|---|
| public long nextLong(long n) Overrides default java. Returns a pseudorandom, uniformly distributed value between 0 (inclusive) and the specified value (exclusive).
| |
| nextLong | back to summary |
|---|---|
| public long nextLong(long least, long bound) Overrides default java. Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
| |
| setSeed | back to summary |
|---|---|
| public void setSeed(long seed) Overrides java. Throws
| |