ThreadLocalRandom

open class ThreadLocalRandom : Random
kotlin.Any
   ↳ java.util.Random
   ↳ java.util.concurrent.ThreadLocalRandom

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 system property java.util.secureRandomSeed is set to true.
Requires API level 21 (Android 5.0, Lollipop)

Summary

Public methods
open Boolean

Returns a pseudorandom boolean value.

open Unit
setSeed(seed: Long)

Throws UnsupportedOperationException.

open Int

Returns a pseudorandom int value.

open Int
nextInt(bound: Int)

Returns a pseudorandom int value between zero (inclusive) and the specified bound (exclusive).

open Int
nextInt(origin: Int, bound: Int)

Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).

open IntStream!
ints(streamSize: Long)

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 Long

Returns a pseudorandom long value.

open Long
nextLong(bound: Long)

Returns a pseudorandom long value between zero (inclusive) and the specified bound (exclusive).

open Long
nextLong(origin: Long, bound: Long)

Returns a pseudorandom long value between the specified origin (inclusive) and the specified bound (exclusive).

open Float

Returns a pseudorandom float value between zero (inclusive) and one (exclusive).

open LongStream!
longs(streamSize: Long)

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!
longs(streamSize: Long, randomNumberOrigin: Long, randomNumberBound: Long)

Returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).

open LongStream!
longs(randomNumberOrigin: Long, randomNumberBound: Long)

Returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).

open Double

Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

open Double

Returns a pseudorandom double value between zero (inclusive) and one (exclusive).

open Double

Returns a pseudorandom double value between 0.

open Double
nextDouble(origin: Double, bound: Double)

Returns a pseudorandom double value between the specified origin (inclusive) and bound (exclusive).

open DoubleStream!
doubles(streamSize: Long)

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!
doubles(streamSize: Long, randomNumberOrigin: Double, randomNumberBound: Double)

Returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

open DoubleStream!
doubles(randomNumberOrigin: Double, randomNumberBound: Double)

Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

open static ThreadLocalRandom!

Returns the current thread's ThreadLocalRandom.

Protected methods
open Int
next(bits: Int)

Generates the next pseudorandom number.

Inherited functions

Public methods

nextBoolean

added in API level 21
open fun nextBoolean(): Boolean

Returns a pseudorandom boolean value.
Requires API level 21 (Android 5.0, Lollipop)

Return
Boolean: a pseudorandom boolean value

setSeed

added in API level 21
open fun setSeed(seed: Long): Unit

Throws UnsupportedOperationException. Setting seeds in this generator is not supported.
Requires API level 21 (Android 5.0, Lollipop)

Parameters
seed Long: the initial seed
Exceptions
UnsupportedOperationException always

nextInt

added in API level 21
open fun nextInt(): Int

Returns a pseudorandom int value.
Requires API level 21 (Android 5.0, Lollipop)

Return
Int: a pseudorandom int value

nextInt

added in API level 21
open fun nextInt(bound: Int): Int

Returns a pseudorandom int value between zero (inclusive) and the specified bound (exclusive).
Requires API level 21 (Android 5.0, Lollipop)

Parameters
bound Int: the upper bound (exclusive). Must be positive.
Return
Int: a pseudorandom int value between zero (inclusive) and the bound (exclusive)
Exceptions
IllegalArgumentException if bound is not positive

nextInt

added in API level 21
open fun nextInt(origin: Int, bound: Int): Int

Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).
Requires API level 21 (Android 5.0, Lollipop)

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
IllegalArgumentException if origin is greater than or equal to bound

ints

added in API level 1.8
open fun ints(streamSize: Long): IntStream!

Returns a stream producing the given streamSize number of pseudorandom int values.
Requires API level 24 (Android 7.0, Nougat)

Parameters
streamSize Long: the number of values to generate
Return
IntStream!: a stream of pseudorandom int values
Exceptions
IllegalArgumentException if streamSize is less than zero

ints

added in API level 1.8
open fun ints(): IntStream!

Returns an effectively unlimited stream of pseudorandom int values.
Requires API level 24 (Android 7.0, Nougat)

Return
IntStream!: a stream of pseudorandom int values

ints

added in API level 1.8
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).
Requires API level 24 (Android 7.0, Nougat)

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
IntStream!: a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive)
Exceptions
IllegalArgumentException if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound

ints

added in API level 1.8
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).
Requires API level 24 (Android 7.0, Nougat)

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
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

nextLong

added in API level 21
open fun nextLong(): Long

Returns a pseudorandom long value.
Requires API level 21 (Android 5.0, Lollipop)

Return
Long: a pseudorandom long value

nextLong

added in API level 21
open fun nextLong(bound: Long): Long

Returns a pseudorandom long value between zero (inclusive) and the specified bound (exclusive).
Requires API level 21 (Android 5.0, Lollipop)

Parameters
bound Long: the upper bound (exclusive). Must be positive.
Return
Long: a pseudorandom long value between zero (inclusive) and the bound (exclusive)
Exceptions
IllegalArgumentException if bound is not positive

nextLong

added in API level 21
open fun nextLong(origin: Long, bound: Long): Long

Returns a pseudorandom long value between the specified origin (inclusive) and the specified bound (exclusive).
Requires API level 21 (Android 5.0, Lollipop)

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
IllegalArgumentException if origin is greater than or equal to bound

nextFloat

added in API level 21
open fun nextFloat(): Float

Returns a pseudorandom float value between zero (inclusive) and one (exclusive).
Requires API level 21 (Android 5.0, Lollipop)

Return
Float: a pseudorandom float value between zero (inclusive) and one (exclusive)

longs

added in API level 1.8
open fun longs(streamSize: Long): LongStream!

Returns a stream producing the given streamSize number of pseudorandom long values.
Requires API level 24 (Android 7.0, Nougat)

Parameters
streamSize Long: the number of values to generate
Return
LongStream!: a stream of pseudorandom long values
Exceptions
IllegalArgumentException if streamSize is less than zero

longs

added in API level 1.8
open fun longs(): LongStream!

Returns an effectively unlimited stream of pseudorandom long values.
Requires API level 24 (Android 7.0, Nougat)

Return
LongStream!: a stream of pseudorandom long values

longs

added in API level 1.8
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).
Requires API level 24 (Android 7.0, Nougat)

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
IllegalArgumentException if streamSize is less than zero, or randomNumberOrigin is greater than or equal to randomNumberBound

longs

added in API level 1.8
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).
Requires API level 24 (Android 7.0, Nougat)

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
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

nextGaussian

added in API level 21
open fun nextGaussian(): Double

Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

The general contract of nextGaussian is that one double value, chosen from (approximately) the usual normal distribution with mean 0.0 and standard deviation 1.0, is pseudorandomly generated and returned.

The method nextGaussian is implemented by class Random as if by a threadsafe version of the following:

 private double nextNextGaussian; private boolean haveNextNextGaussian = false; public double nextGaussian() { if (haveNextNextGaussian) { haveNextNextGaussian = false; return nextNextGaussian; } else { double v1, v2, s; do { v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0 v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0 s = v1 * v1 + v2 * v2; } while (s >= 1 || s == 0); double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); nextNextGaussian = v2 * multiplier; haveNextNextGaussian = true; return v1 * multiplier; } }
This uses the polar method of G. E. P. Box, M. E. Muller, and G. Marsaglia, as described by Donald E. Knuth in The Art of Computer Programming, Volume 3: Seminumerical Algorithms, section 3.4.1, subsection C, algorithm P. Note that it generates two independent values at the cost of only one call to StrictMath.log and one call to StrictMath.sqrt. Requires API level 21 (Android 5.0, Lollipop)
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

nextDouble

added in API level 21
open fun nextDouble(): Double

Returns a pseudorandom double value between zero (inclusive) and one (exclusive).
Requires API level 21 (Android 5.0, Lollipop)

Return
Double: a pseudorandom double value between zero (inclusive) and one (exclusive)

nextDouble

added in API level 21
open fun nextDouble(bound: Double): Double

Returns a pseudorandom double value between 0.0 (inclusive) and the specified bound (exclusive).
Requires API level 21 (Android 5.0, Lollipop)

Parameters
bound Double: the upper bound (exclusive). Must be positive.
Return
Double: a pseudorandom double value between zero (inclusive) and the bound (exclusive)
Exceptions
IllegalArgumentException if bound is not positive

nextDouble

added in API level 21
open fun nextDouble(origin: Double, bound: Double): Double

Returns a pseudorandom double value between the specified origin (inclusive) and bound (exclusive).
Requires API level 21 (Android 5.0, Lollipop)

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
IllegalArgumentException if origin is greater than or equal to bound

doubles

added in API level 1.8
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).
Requires API level 24 (Android 7.0, Nougat)

Parameters
streamSize Long: the number of values to generate
Return
DoubleStream!: a stream of double values
Exceptions
IllegalArgumentException if streamSize is less than zero

doubles

added in API level 1.8
open fun doubles(): DoubleStream!

Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).
Requires API level 24 (Android 7.0, Nougat)

Return
DoubleStream!: a stream of pseudorandom double values

doubles

added in API level 1.8
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).
Requires API level 24 (Android 7.0, Nougat)

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
DoubleStream!: a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)
Exceptions
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

doubles

added in API level 1.8
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).
Requires API level 24 (Android 7.0, Nougat)

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
IllegalArgumentException if randomNumberOrigin is greater than or equal to randomNumberBound

current

added in API level 21
open static fun current(): ThreadLocalRandom!

Returns the current thread's ThreadLocalRandom.
Requires API level 21 (Android 5.0, Lollipop)

Return
ThreadLocalRandom!: the current thread's ThreadLocalRandom

Protected methods

next

added in API level 21
protected open fun next(bits: Int): Int

Generates the next pseudorandom number. Subclasses should override this, as this is used by all other methods.

The general contract of next is that it returns an int value and if the argument bits is between 1 and 32 (inclusive), then that many low-order bits of the returned value will be (approximately) independently chosen bit values, each of which is (approximately) equally likely to be 0 or 1. The method next is implemented by class Random by atomically updating the seed to

(seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)
and returning
(int)(seed >>> (48 - bits)).
This is a linear congruential pseudorandom number generator, as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 3: Seminumerical Algorithms, section 3.2.1. Requires API level 21 (Android 5.0, Lollipop)
Parameters
bits Int: random bits
Return
Int: the next pseudorandom value from this random number generator's sequence