Class Overview
A random number generator isolated to the current thread. Like the
global Random
generator used by the 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.
Summary
Public Methods |
static
ThreadLocalRandom
|
current()
Returns the current thread's ThreadLocalRandom .
|
double
|
nextDouble(double n)
Returns a pseudorandom, uniformly distributed double value
between 0 (inclusive) and the specified value (exclusive).
|
double
|
nextDouble(double least, double bound)
Returns a pseudorandom, uniformly distributed value between the
given least value (inclusive) and bound (exclusive).
|
int
|
nextInt(int least, int bound)
Returns a pseudorandom, uniformly distributed value between the
given least value (inclusive) and bound (exclusive).
|
long
|
nextLong(long n)
Returns a pseudorandom, uniformly distributed value
between 0 (inclusive) and the specified value (exclusive).
|
long
|
nextLong(long least, long bound)
Returns a pseudorandom, uniformly distributed value between the
given least value (inclusive) and bound (exclusive).
|
void
|
setSeed(long seed)
Throws UnsupportedOperationException .
|
Protected Methods |
int
|
next(int bits)
Returns a pseudo-random uniformly distributed int value of
the number of bits specified by the argument bits as
described by Donald E.
|
[Expand]
Inherited Methods |
From class
java.util.Random
int
|
next(int bits)
Returns a pseudo-random uniformly distributed int value of
the number of bits specified by the argument bits as
described by Donald E.
|
boolean
|
nextBoolean()
Returns a pseudo-random uniformly distributed boolean .
|
void
|
nextBytes(byte[] buf)
Fills buf with random bytes.
|
double
|
nextDouble()
Returns a pseudo-random uniformly distributed double
in the half-open range [0.0, 1.0).
|
float
|
nextFloat()
Returns a pseudo-random uniformly distributed float
in the half-open range [0.0, 1.0).
|
double
|
nextGaussian()
Returns a pseudo-random (approximately) normally distributed
double with mean 0.0 and standard deviation 1.0.
|
int
|
nextInt(int n)
Returns a pseudo-random uniformly distributed int
in the half-open range [0, n).
|
int
|
nextInt()
Returns a pseudo-random uniformly distributed int .
|
long
|
nextLong()
Returns a pseudo-random uniformly distributed long .
|
void
|
setSeed(long seed)
Modifies the seed using a linear congruential formula presented in The
Art of Computer Programming, Volume 2, Section 3.2.1.
|
|
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals(Object o)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
final
void
|
wait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
|
Public Methods
Returns the current thread's ThreadLocalRandom
.
public
double
nextDouble
(double n)
Returns a pseudorandom, uniformly distributed double
value
between 0 (inclusive) and the specified value (exclusive).
Parameters |
n |
double :
the bound on the random number to be returned. Must be
positive. |
Returns |
double |
the next value |
public
double
nextDouble
(double least, double bound)
Returns a pseudorandom, uniformly distributed value between the
given least value (inclusive) and bound (exclusive).
Parameters |
least |
double :
the least value returned |
bound |
double :
the upper bound (exclusive) |
Returns |
double |
the next value |
public
int
nextInt
(int least, int bound)
Returns a pseudorandom, uniformly distributed value between the
given least value (inclusive) and bound (exclusive).
Parameters |
least |
int :
the least value returned |
bound |
int :
the upper bound (exclusive) |
Returns |
int |
the next value |
public
long
nextLong
(long n)
Returns a pseudorandom, uniformly distributed value
between 0 (inclusive) and the specified value (exclusive).
Parameters |
n |
long :
the bound on the random number to be returned. Must be
positive. |
Returns |
long |
the next value |
public
long
nextLong
(long least, long bound)
Returns a pseudorandom, uniformly distributed value between the
given least value (inclusive) and bound (exclusive).
Parameters |
least |
long :
the least value returned |
bound |
long :
the upper bound (exclusive) |
Returns |
long |
the next value |
public
void
setSeed
(long seed)
Throws UnsupportedOperationException
. Setting seeds in
this generator is not supported.
Protected Methods
protected
int
next
(int bits)
Returns a pseudo-random uniformly distributed int
value of
the number of bits specified by the argument bits
as
described by Donald E. Knuth in The Art of Computer Programming,
Volume 2: Seminumerical Algorithms, section 3.2.1.
Most applications will want to use one of this class' convenience methods instead.
Subclasses only need to override this method to alter the behavior
of all the public methods.