java.lang.Object
java.util.Random
java.util.concurrent.ThreadLocalRandom
- All Implemented Interfaces:
Serializable
public class ThreadLocalRandom extends Random
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
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 SecureRandom
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.
- Since:
- 1.7
- See Also:
- Serialized Form
-
Method Summary
Modifier and Type Method Description static ThreadLocalRandomcurrent()Returns the current thread'sThreadLocalRandom.DoubleStreamdoubles()Returns an effectively unlimited stream of pseudorandomdoublevalues, each between zero (inclusive) and one (exclusive).DoubleStreamdoubles(double randomNumberOrigin, double randomNumberBound)Returns an effectively unlimited stream of pseudorandomdoublevalues, each conforming to the given origin (inclusive) and bound (exclusive).DoubleStreamdoubles(long streamSize)Returns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues, each between zero (inclusive) and one (exclusive).DoubleStreamdoubles(long streamSize, double randomNumberOrigin, double randomNumberBound)Returns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues, each conforming to the given origin (inclusive) and bound (exclusive).IntStreamints()Returns an effectively unlimited stream of pseudorandomintvalues.IntStreamints(int randomNumberOrigin, int randomNumberBound)Returns an effectively unlimited stream of pseudorandomintvalues, each conforming to the given origin (inclusive) and bound (exclusive).IntStreamints(long streamSize)Returns a stream producing the givenstreamSizenumber of pseudorandomintvalues.IntStreamints(long streamSize, int randomNumberOrigin, int randomNumberBound)Returns a stream producing the givenstreamSizenumber of pseudorandomintvalues, each conforming to the given origin (inclusive) and bound (exclusive).LongStreamlongs()Returns an effectively unlimited stream of pseudorandomlongvalues.LongStreamlongs(long streamSize)Returns a stream producing the givenstreamSizenumber of pseudorandomlongvalues.LongStreamlongs(long randomNumberOrigin, long randomNumberBound)Returns an effectively unlimited stream of pseudorandomlongvalues, each conforming to the given origin (inclusive) and bound (exclusive).LongStreamlongs(long streamSize, long randomNumberOrigin, long randomNumberBound)Returns a stream producing the givenstreamSizenumber of pseudorandomlong, each conforming to the given origin (inclusive) and bound (exclusive).protected intnext(int bits)Generates a pseudorandom number with the indicated number of low-order bits.booleannextBoolean()Returns a pseudorandombooleanvalue.doublenextDouble()Returns a pseudorandomdoublevalue between zero (inclusive) and one (exclusive).doublenextDouble(double bound)Returns a pseudorandomdoublevalue between 0.0 (inclusive) and the specified bound (exclusive).doublenextDouble(double origin, double bound)Returns a pseudorandomdoublevalue between the specified origin (inclusive) and bound (exclusive).floatnextFloat()Returns a pseudorandomfloatvalue between zero (inclusive) and one (exclusive).intnextInt()Returns a pseudorandomintvalue.intnextInt(int bound)Returns a pseudorandomintvalue between zero (inclusive) and the specified bound (exclusive).intnextInt(int origin, int bound)Returns a pseudorandomintvalue between the specified origin (inclusive) and the specified bound (exclusive).longnextLong()Returns a pseudorandomlongvalue.longnextLong(long bound)Returns a pseudorandomlongvalue between zero (inclusive) and the specified bound (exclusive).longnextLong(long origin, long bound)Returns a pseudorandomlongvalue between the specified origin (inclusive) and the specified bound (exclusive).voidsetSeed(long seed)ThrowsUnsupportedOperationException.
-
Method Details
-
current
Returns the current thread'sThreadLocalRandom.- Returns:
- the current thread's
ThreadLocalRandom
-
setSeed
public void setSeed(long seed)ThrowsUnsupportedOperationException. Setting seeds in this generator is not supported.- Overrides:
setSeedin classRandom- Parameters:
seed- the initial seed- Throws:
UnsupportedOperationException- always
-
next
protected int next(int bits)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. -
nextInt
public int nextInt()Returns a pseudorandomintvalue. -
nextInt
public int nextInt(int bound)Returns a pseudorandomintvalue between zero (inclusive) and the specified bound (exclusive).- Overrides:
nextIntin classRandom- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
intvalue between zero (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- ifboundis not positive
-
nextInt
public int nextInt(int origin, int bound)Returns a pseudorandomintvalue between the specified origin (inclusive) and the specified bound (exclusive).- Parameters:
origin- the least value returnedbound- the upper bound (exclusive)- Returns:
- a pseudorandom
intvalue between the origin (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- iforiginis greater than or equal tobound
-
nextLong
public long nextLong()Returns a pseudorandomlongvalue. -
nextLong
public long nextLong(long bound)Returns a pseudorandomlongvalue between zero (inclusive) and the specified bound (exclusive).- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
longvalue between zero (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- ifboundis not positive
-
nextLong
public long nextLong(long origin, long bound)Returns a pseudorandomlongvalue between the specified origin (inclusive) and the specified bound (exclusive).- Parameters:
origin- the least value returnedbound- the upper bound (exclusive)- Returns:
- a pseudorandom
longvalue between the origin (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- iforiginis greater than or equal tobound
-
nextDouble
public double nextDouble()Returns a pseudorandomdoublevalue between zero (inclusive) and one (exclusive).- Overrides:
nextDoublein classRandom- Returns:
- a pseudorandom
doublevalue between zero (inclusive) and one (exclusive) - See Also:
Math.random()
-
nextDouble
public double nextDouble(double bound)Returns a pseudorandomdoublevalue between 0.0 (inclusive) and the specified bound (exclusive).- Parameters:
bound- the upper bound (exclusive). Must be positive.- Returns:
- a pseudorandom
doublevalue between zero (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- ifboundis not positive
-
nextDouble
public double nextDouble(double origin, double bound)Returns a pseudorandomdoublevalue between the specified origin (inclusive) and bound (exclusive).- Parameters:
origin- the least value returnedbound- the upper bound (exclusive)- Returns:
- a pseudorandom
doublevalue between the origin (inclusive) and the bound (exclusive) - Throws:
IllegalArgumentException- iforiginis greater than or equal tobound
-
nextBoolean
public boolean nextBoolean()Returns a pseudorandombooleanvalue.- Overrides:
nextBooleanin classRandom- Returns:
- a pseudorandom
booleanvalue
-
nextFloat
public float nextFloat()Returns a pseudorandomfloatvalue between zero (inclusive) and one (exclusive). -
ints
Returns a stream producing the givenstreamSizenumber of pseudorandomintvalues.- Overrides:
intsin classRandom- Parameters:
streamSize- the number of values to generate- Returns:
- a stream of pseudorandom
intvalues - Throws:
IllegalArgumentException- ifstreamSizeis less than zero- Since:
- 1.8
-
ints
Returns an effectively unlimited stream of pseudorandomintvalues. -
ints
Returns a stream producing the givenstreamSizenumber of pseudorandomintvalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
intsin classRandom- Parameters:
streamSize- the number of values to generaterandomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- the bound (exclusive) of each random value- Returns:
- a stream of pseudorandom
intvalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
IllegalArgumentException- ifstreamSizeis less than zero, orrandomNumberOriginis greater than or equal torandomNumberBound- Since:
- 1.8
-
ints
Returns an effectively unlimited stream of pseudorandomintvalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
intsin classRandom- Implementation Note:
- This method is implemented to be equivalent to
ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
randomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- the bound (exclusive) of each random value- Returns:
- a stream of pseudorandom
intvalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
IllegalArgumentException- ifrandomNumberOriginis greater than or equal torandomNumberBound- Since:
- 1.8
-
longs
Returns a stream producing the givenstreamSizenumber of pseudorandomlongvalues.- Overrides:
longsin classRandom- Parameters:
streamSize- the number of values to generate- Returns:
- a stream of pseudorandom
longvalues - Throws:
IllegalArgumentException- ifstreamSizeis less than zero- Since:
- 1.8
-
longs
Returns an effectively unlimited stream of pseudorandomlongvalues. -
longs
Returns a stream producing the givenstreamSizenumber of pseudorandomlong, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
longsin classRandom- Parameters:
streamSize- the number of values to generaterandomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- the bound (exclusive) of each random value- Returns:
- a stream of pseudorandom
longvalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
IllegalArgumentException- ifstreamSizeis less than zero, orrandomNumberOriginis greater than or equal torandomNumberBound- Since:
- 1.8
-
longs
Returns an effectively unlimited stream of pseudorandomlongvalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
longsin classRandom- Implementation Note:
- This method is implemented to be equivalent to
longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
randomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- the bound (exclusive) of each random value- Returns:
- a stream of pseudorandom
longvalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
IllegalArgumentException- ifrandomNumberOriginis greater than or equal torandomNumberBound- Since:
- 1.8
-
doubles
Returns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues, each between zero (inclusive) and one (exclusive).- Overrides:
doublesin classRandom- Parameters:
streamSize- the number of values to generate- Returns:
- a stream of
doublevalues - Throws:
IllegalArgumentException- ifstreamSizeis less than zero- Since:
- 1.8
-
doubles
Returns an effectively unlimited stream of pseudorandomdoublevalues, each between zero (inclusive) and one (exclusive). -
doubles
Returns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
doublesin classRandom- Parameters:
streamSize- the number of values to generaterandomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- the bound (exclusive) of each random value- Returns:
- a stream of pseudorandom
doublevalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
IllegalArgumentException- ifstreamSizeis less than zero, orrandomNumberOriginis greater than or equal torandomNumberBound- Since:
- 1.8
-
doubles
Returns an effectively unlimited stream of pseudorandomdoublevalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
doublesin classRandom- Implementation Note:
- This method is implemented to be equivalent to
doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound). - Parameters:
randomNumberOrigin- the origin (inclusive) of each random valuerandomNumberBound- the bound (exclusive) of each random value- Returns:
- a stream of pseudorandom
doublevalues, each with the given origin (inclusive) and bound (exclusive) - Throws:
IllegalArgumentException- ifrandomNumberOriginis greater than or equal torandomNumberBound- Since:
- 1.8
-