- 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 globalRandomgenerator used by theMathclass, aThreadLocalRandomis initialized with an internally generated seed that may not otherwise be modified. When applicable, use ofThreadLocalRandomrather than sharedRandomobjects in concurrent programs will typically encounter much less overhead and contention. Use ofThreadLocalRandomis particularly appropriate when multiple tasks (for example, each aForkJoinTask) use random numbers in parallel in thread pools.Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(...)(whereXisInt,Long, etc). When all usages are of this form, it is never possible to accidentally share aThreadLocalRandomacross multiple threads.This class also provides additional commonly used bounded random generation methods. Instances of ThreadLocalRandomare not cryptographically secure. Consider instead usingSecureRandomin security-sensitive applications. Additionally, default-constructed instances do not use a cryptographically random seed unless the system propertyjava.util.secureRandomSeedis set totrue.- Since:
- 1.7
- See Also:
- Serialized Form
 
- 
- 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods 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.- 
Methods declared in class java.util.RandomnextBytes, nextGaussian
 
- 
 
- 
- 
- 
Method Detail- 
currentpublic static ThreadLocalRandom current() Returns the current thread'sThreadLocalRandom.- Returns:
- the current thread's ThreadLocalRandom
 
 - 
setSeedpublic void setSeed(long seed) ThrowsUnsupportedOperationException. Setting seeds in this generator is not supported.- Overrides:
- setSeedin class- Random
- Parameters:
- seed- the initial seed
- Throws:
- UnsupportedOperationException- always
 
 - 
nextprotected 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.
 - 
nextIntpublic int nextInt() Returns a pseudorandomintvalue.
 - 
nextIntpublic int nextInt(int bound) Returns a pseudorandomintvalue between zero (inclusive) and the specified bound (exclusive).- Overrides:
- nextIntin class- Random
- Parameters:
- bound- the upper bound (exclusive). Must be positive.
- Returns:
- a pseudorandom intvalue between zero (inclusive) and the bound (exclusive)
- Throws:
- IllegalArgumentException- if- boundis not positive
 
 - 
nextIntpublic int nextInt(int origin, int bound)Returns a pseudorandomintvalue between the specified origin (inclusive) and the specified bound (exclusive).- Parameters:
- origin- the least value returned
- bound- the upper bound (exclusive)
- Returns:
- a pseudorandom intvalue between the origin (inclusive) and the bound (exclusive)
- Throws:
- IllegalArgumentException- if- originis greater than or equal to- bound
 
 - 
nextLongpublic long nextLong() Returns a pseudorandomlongvalue.
 - 
nextLongpublic 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- if- boundis not positive
 
 - 
nextLongpublic long nextLong(long origin, long bound)Returns a pseudorandomlongvalue between the specified origin (inclusive) and the specified bound (exclusive).- Parameters:
- origin- the least value returned
- bound- the upper bound (exclusive)
- Returns:
- a pseudorandom longvalue between the origin (inclusive) and the bound (exclusive)
- Throws:
- IllegalArgumentException- if- originis greater than or equal to- bound
 
 - 
nextDoublepublic double nextDouble() Returns a pseudorandomdoublevalue between zero (inclusive) and one (exclusive).- Overrides:
- nextDoublein class- Random
- Returns:
- a pseudorandom doublevalue between zero (inclusive) and one (exclusive)
- See Also:
- Math.random()
 
 - 
nextDoublepublic 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- if- boundis not positive
 
 - 
nextDoublepublic double nextDouble(double origin, double bound)Returns a pseudorandomdoublevalue between the specified origin (inclusive) and bound (exclusive).- Parameters:
- origin- the least value returned
- bound- the upper bound (exclusive)
- Returns:
- a pseudorandom doublevalue between the origin (inclusive) and the bound (exclusive)
- Throws:
- IllegalArgumentException- if- originis greater than or equal to- bound
 
 - 
nextBooleanpublic boolean nextBoolean() Returns a pseudorandombooleanvalue.- Overrides:
- nextBooleanin class- Random
- Returns:
- a pseudorandom booleanvalue
 
 - 
nextFloatpublic float nextFloat() Returns a pseudorandomfloatvalue between zero (inclusive) and one (exclusive).
 - 
intspublic IntStream ints(long streamSize) Returns a stream producing the givenstreamSizenumber of pseudorandomintvalues.- Overrides:
- intsin class- Random
- Parameters:
- streamSize- the number of values to generate
- Returns:
- a stream of pseudorandom intvalues
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero
- Since:
- 1.8
 
 - 
intspublic IntStream ints() Returns an effectively unlimited stream of pseudorandomintvalues.
 - 
intspublic IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) Returns a stream producing the givenstreamSizenumber of pseudorandomintvalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
- intsin class- Random
- Parameters:
- streamSize- the number of values to generate
- randomNumberOrigin- the origin (inclusive) of each random value
- randomNumberBound- the bound (exclusive) of each random value
- Returns:
- a stream of pseudorandom intvalues, each with the given origin (inclusive) and bound (exclusive)
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero, or- randomNumberOriginis greater than or equal to- randomNumberBound
- Since:
- 1.8
 
 - 
intspublic IntStream ints(int randomNumberOrigin, int randomNumberBound) Returns an effectively unlimited stream of pseudorandomintvalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
- intsin class- Random
- Implementation Note:
- This method is implemented to be equivalent to ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound).
- Parameters:
- randomNumberOrigin- the origin (inclusive) of each random value
- randomNumberBound- the bound (exclusive) of each random value
- Returns:
- a stream of pseudorandom intvalues, each with the given origin (inclusive) and bound (exclusive)
- Throws:
- IllegalArgumentException- if- randomNumberOriginis greater than or equal to- randomNumberBound
- Since:
- 1.8
 
 - 
longspublic LongStream longs(long streamSize) Returns a stream producing the givenstreamSizenumber of pseudorandomlongvalues.- Overrides:
- longsin class- Random
- Parameters:
- streamSize- the number of values to generate
- Returns:
- a stream of pseudorandom longvalues
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero
- Since:
- 1.8
 
 - 
longspublic LongStream longs() Returns an effectively unlimited stream of pseudorandomlongvalues.
 - 
longspublic LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) Returns a stream producing the givenstreamSizenumber of pseudorandomlong, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
- longsin class- Random
- Parameters:
- streamSize- the number of values to generate
- randomNumberOrigin- the origin (inclusive) of each random value
- randomNumberBound- the bound (exclusive) of each random value
- Returns:
- a stream of pseudorandom longvalues, each with the given origin (inclusive) and bound (exclusive)
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero, or- randomNumberOriginis greater than or equal to- randomNumberBound
- Since:
- 1.8
 
 - 
longspublic LongStream longs(long randomNumberOrigin, long randomNumberBound) Returns an effectively unlimited stream of pseudorandomlongvalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
- longsin class- Random
- Implementation Note:
- This method is implemented to be equivalent to longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound).
- Parameters:
- randomNumberOrigin- the origin (inclusive) of each random value
- randomNumberBound- the bound (exclusive) of each random value
- Returns:
- a stream of pseudorandom longvalues, each with the given origin (inclusive) and bound (exclusive)
- Throws:
- IllegalArgumentException- if- randomNumberOriginis greater than or equal to- randomNumberBound
- Since:
- 1.8
 
 - 
doublespublic DoubleStream doubles(long streamSize) Returns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues, each between zero (inclusive) and one (exclusive).- Overrides:
- doublesin class- Random
- Parameters:
- streamSize- the number of values to generate
- Returns:
- a stream of doublevalues
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero
- Since:
- 1.8
 
 - 
doublespublic DoubleStream doubles() Returns an effectively unlimited stream of pseudorandomdoublevalues, each between zero (inclusive) and one (exclusive).
 - 
doublespublic DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) Returns a stream producing the givenstreamSizenumber of pseudorandomdoublevalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
- doublesin class- Random
- Parameters:
- streamSize- the number of values to generate
- randomNumberOrigin- the origin (inclusive) of each random value
- randomNumberBound- the bound (exclusive) of each random value
- Returns:
- a stream of pseudorandom doublevalues, each with the given origin (inclusive) and bound (exclusive)
- Throws:
- IllegalArgumentException- if- streamSizeis less than zero, or- randomNumberOriginis greater than or equal to- randomNumberBound
- Since:
- 1.8
 
 - 
doublespublic DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) Returns an effectively unlimited stream of pseudorandomdoublevalues, each conforming to the given origin (inclusive) and bound (exclusive).- Overrides:
- doublesin class- Random
- Implementation Note:
- This method is implemented to be equivalent to doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound).
- Parameters:
- randomNumberOrigin- the origin (inclusive) of each random value
- randomNumberBound- the bound (exclusive) of each random value
- Returns:
- a stream of pseudorandom doublevalues, each with the given origin (inclusive) and bound (exclusive)
- Throws:
- IllegalArgumentException- if- randomNumberOriginis greater than or equal to- randomNumberBound
- Since:
- 1.8
 
 
- 
 
-