ThreadLocalRandom
open class ThreadLocalRandom : Random
A random number generator isolated to the current thread. Like the global java.util.Random generator used by the 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.
Instances of ThreadLocalRandom are not cryptographically secure. Consider instead using java.security.SecureRandom in security-sensitive applications. Additionally, default-constructed instances do not use a cryptographically random seed unless the java.lang.System#getProperty java.util.secureRandomSeed is set to true.
Summary
| Public methods |
| open static ThreadLocalRandom! |
Returns the current thread's ThreadLocalRandom.
|
| open DoubleStream! |
Returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).
|
| open DoubleStream! |
Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).
|
| open DoubleStream! |
Returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
|
| open DoubleStream! |
Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
|
| open IntStream! |
Returns a stream producing the given streamSize number of pseudorandom int values.
|
| open IntStream! |
Returns an effectively unlimited stream of pseudorandom int values.
|
| open IntStream! |
ints(streamSize: Long, randomNumberOrigin: Int, randomNumberBound: Int)
Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
|
| open IntStream! |
ints(randomNumberOrigin: Int, randomNumberBound: Int)
Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
|
| open LongStream! |
Returns a stream producing the given streamSize number of pseudorandom long values.
|
| open LongStream! |
Returns an effectively unlimited stream of pseudorandom long values.
|
| open LongStream! |
Returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).
|
| open LongStream! |
Returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).
|
| open Boolean |
Returns a pseudorandom boolean value.
|
| open Double |
Returns a pseudorandom double value between zero (inclusive) and one (exclusive).
|
| open Double |
Returns a pseudorandom double value between 0.
|
| open Double |
Returns a pseudorandom double value between the specified origin (inclusive) and bound (exclusive).
|
| open Float |
Returns a pseudorandom float value between zero (inclusive) and one (exclusive).
|
| open Double |
|
| open Int |
Returns a pseudorandom int value.
|
| open Int |
Returns a pseudorandom int value between zero (inclusive) and the specified bound (exclusive).
|
| open Int |
Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).
|
| open Long |
Returns a pseudorandom long value.
|
| open Long |
Returns a pseudorandom long value between zero (inclusive) and the specified bound (exclusive).
|
| open Long |
Returns a pseudorandom long value between the specified origin (inclusive) and the specified bound (exclusive).
|
| open Unit |
Throws UnsupportedOperationException.
|
| Protected methods |
| open Int |
|
| Inherited functions |
From class Random
Unit |
nextBytes(bytes: ByteArray!)
Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.
The method nextBytes is implemented by class Random as if by:
<code>public void nextBytes(byte[] bytes) {
for (int i = 0; i < bytes.length; )
for (int rnd = nextInt(), n = Math.min(bytes.length - i, 4);
n-- > 0; rnd >>= 8)
bytes[i++] = (byte)rnd;
}</code>
|
|
Public methods
current
open static fun current(): ThreadLocalRandom!
Returns the current thread's ThreadLocalRandom.
doubles
open fun doubles(streamSize: Long): DoubleStream!
Returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).
| Parameters |
streamSize |
Long: the number of values to generate |
| Exceptions |
java.lang.IllegalArgumentException |
if streamSize is less than zero |
doubles
open fun doubles(): DoubleStream!
Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).
doubles
open fun doubles(
streamSize: Long,
randomNumberOrigin: Double,
randomNumberBound: Double
): DoubleStream!
Returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
| Parameters |
streamSize |
Long: the number of values to generate |
randomNumberOrigin |
Double: the origin (inclusive) of each random value |
randomNumberBound |
Double: the bound (exclusive) of each random value |
| Return |
DoubleStream! |
a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if randomNumberOrigin is greater than or equal to randomNumberBound |
doubles
open fun doubles(
randomNumberOrigin: Double,
randomNumberBound: Double
): DoubleStream!
Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
| Parameters |
randomNumberOrigin |
Double: the origin (inclusive) of each random value |
randomNumberBound |
Double: the bound (exclusive) of each random value |
| Return |
DoubleStream! |
a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if randomNumberOrigin is greater than or equal to randomNumberBound |
ints
open fun ints(streamSize: Long): IntStream!
Returns a stream producing the given streamSize number of pseudorandom int values.
| Parameters |
streamSize |
Long: the number of values to generate |
| Return |
IntStream! |
a stream of pseudorandom int values |
| Exceptions |
java.lang.IllegalArgumentException |
if streamSize is less than zero |
ints
open fun ints(): IntStream!
Returns an effectively unlimited stream of pseudorandom int values.
| Return |
IntStream! |
a stream of pseudorandom int values |
ints
open fun ints(
streamSize: Long,
randomNumberOrigin: Int,
randomNumberBound: Int
): IntStream!
Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
| Parameters |
streamSize |
Long: the number of values to generate |
randomNumberOrigin |
Int: the origin (inclusive) of each random value |
randomNumberBound |
Int: the bound (exclusive) of each random value |
| Return |
IntStream! |
a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound |
ints
open fun ints(
randomNumberOrigin: Int,
randomNumberBound: Int
): IntStream!
Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).
| Parameters |
randomNumberOrigin |
Int: the origin (inclusive) of each random value |
randomNumberBound |
Int: the bound (exclusive) of each random value |
| Return |
IntStream! |
a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if randomNumberOrigin is greater than or equal to randomNumberBound |
longs
open fun longs(streamSize: Long): LongStream!
Returns a stream producing the given streamSize number of pseudorandom long values.
| Parameters |
streamSize |
Long: the number of values to generate |
| Return |
LongStream! |
a stream of pseudorandom long values |
| Exceptions |
java.lang.IllegalArgumentException |
if streamSize is less than zero |
longs
open fun longs(): LongStream!
Returns an effectively unlimited stream of pseudorandom long values.
| Return |
LongStream! |
a stream of pseudorandom long values |
longs
open fun longs(
streamSize: Long,
randomNumberOrigin: Long,
randomNumberBound: Long
): LongStream!
Returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).
| Parameters |
streamSize |
Long: the number of values to generate |
randomNumberOrigin |
Long: the origin (inclusive) of each random value |
randomNumberBound |
Long: the bound (exclusive) of each random value |
| Return |
LongStream! |
a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound |
longs
open fun longs(
randomNumberOrigin: Long,
randomNumberBound: Long
): LongStream!
Returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).
| Parameters |
randomNumberOrigin |
Long: the origin (inclusive) of each random value |
randomNumberBound |
Long: the bound (exclusive) of each random value |
| Return |
LongStream! |
a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if randomNumberOrigin is greater than or equal to randomNumberBound |
nextBoolean
open fun nextBoolean(): Boolean
Returns a pseudorandom boolean value.
| Return |
Boolean |
a pseudorandom boolean value |
nextDouble
open fun nextDouble(): Double
Returns a pseudorandom double value between zero (inclusive) and one (exclusive).
| Return |
Double |
a pseudorandom double value between zero (inclusive) and one (exclusive) |
nextDouble
open fun nextDouble(bound: Double): Double
Returns a pseudorandom double value between 0.0 (inclusive) and the specified bound (exclusive).
| Parameters |
bound |
Double: the upper bound (exclusive). Must be positive. |
| Return |
Double |
a pseudorandom double value between zero (inclusive) and the bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if bound is not positive |
nextDouble
open fun nextDouble(
origin: Double,
bound: Double
): Double
Returns a pseudorandom double value between the specified origin (inclusive) and bound (exclusive).
| Parameters |
origin |
Double: the least value returned |
bound |
Double: the upper bound (exclusive) |
| Return |
Double |
a pseudorandom double value between the origin (inclusive) and the bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if origin is greater than or equal to bound |
nextFloat
open fun nextFloat(): Float
Returns a pseudorandom float value between zero (inclusive) and one (exclusive).
| Return |
Float |
a pseudorandom float value between zero (inclusive) and one (exclusive) |
nextGaussian
open fun nextGaussian(): Double
| Return |
Double |
the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence |
nextInt
open fun nextInt(): Int
Returns a pseudorandom int value.
| Return |
Int |
a pseudorandom int value |
nextInt
open fun nextInt(bound: Int): Int
Returns a pseudorandom int value between zero (inclusive) and the specified bound (exclusive).
| Parameters |
bound |
Int: the upper bound (exclusive). Must be positive. |
| Return |
Int |
a pseudorandom int value between zero (inclusive) and the bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if bound is not positive |
nextInt
open fun nextInt(
origin: Int,
bound: Int
): Int
Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).
| Parameters |
origin |
Int: the least value returned |
bound |
Int: the upper bound (exclusive) |
| Return |
Int |
a pseudorandom int value between the origin (inclusive) and the bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if origin is greater than or equal to bound |
nextLong
open fun nextLong(): Long
Returns a pseudorandom long value.
| Return |
Long |
a pseudorandom long value |
nextLong
open fun nextLong(bound: Long): Long
Returns a pseudorandom long value between zero (inclusive) and the specified bound (exclusive).
| Parameters |
bound |
Long: the upper bound (exclusive). Must be positive. |
| Return |
Long |
a pseudorandom long value between zero (inclusive) and the bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if bound is not positive |
nextLong
open fun nextLong(
origin: Long,
bound: Long
): Long
Returns a pseudorandom long value between the specified origin (inclusive) and the specified bound (exclusive).
| Parameters |
origin |
Long: the least value returned |
bound |
Long: the upper bound (exclusive) |
| Return |
Long |
a pseudorandom long value between the origin (inclusive) and the bound (exclusive) |
| Exceptions |
java.lang.IllegalArgumentException |
if origin is greater than or equal to bound |
setSeed
open fun setSeed(seed: Long): Unit
Throws UnsupportedOperationException. Setting seeds in this generator is not supported.
| Parameters |
seed |
Long: the initial seed |
| Exceptions |
java.lang.UnsupportedOperationException |
always |
Protected methods
next
protected open fun next(bits: Int): Int
| Parameters |
bits |
Int: random bits |
| Return |
Int |
the next pseudorandom value from this random number generator's sequence |