Added in API level 21

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 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!
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 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 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 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
nextDouble(origin: Double, bound: 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
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 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 Unit
setSeed(seed: Long)

Throws UnsupportedOperationException.

Protected methods
open Int
next(bits: Int)

Inherited functions

Public methods

current

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

Returns the current thread's ThreadLocalRandom.

Return
ThreadLocalRandom! the current thread's ThreadLocalRandom

doubles

Added in API level 24
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
Return
DoubleStream! a stream of double values
Exceptions
java.lang.IllegalArgumentException if streamSize is less than zero

doubles

Added in API level 24
open fun doubles(): DoubleStream!

Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).

Return
DoubleStream! a stream of pseudorandom double values

doubles

Added in API level 24
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

Added in API level 24
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

Added in API level 24
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

Added in API level 24
open fun ints(): IntStream!

Returns an effectively unlimited stream of pseudorandom int values.

Return
IntStream! a stream of pseudorandom int values

ints

Added in API level 24
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

Added in API level 24
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

Added in API level 24
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

Added in API level 24
open fun longs(): LongStream!

Returns an effectively unlimited stream of pseudorandom long values.

Return
LongStream! a stream of pseudorandom long values

longs

Added in API level 24
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

Added in API level 24
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

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

Returns a pseudorandom boolean value.

Return
Boolean a pseudorandom boolean value

nextDouble

Added in API level 21
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

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).

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

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).

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

Added in API level 21
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

Added in API level 21
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

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

Returns a pseudorandom int value.

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).

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

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).

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

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

Returns a pseudorandom long value.

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).

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

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).

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

Added in API level 21
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

Added in API level 21
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