Default

The default random number generator.

On JVM this generator is thread-safe, its methods can be invoked from multiple threads.

Since Kotlin

1.3

Samples

import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   val randomValues = List(10) { Random.nextInt(0, 100) }
// prints new sequence every time
println(randomValues)

val nextValues = List(10) { Random.nextInt(0, 100) }
println(nextValues)
println("randomValues != nextValues is ${randomValues != nextValues}") // true 
   //sampleEnd
}

Functions

Link copied to clipboard

Creates a java.util.Random instance that uses the specified Kotlin Random generator as a randomness source.

Since Kotlin 1.3
Link copied to clipboard
open override fun nextBits(bitCount: Int): Int

Gets the next random bitCount number of bits.

Since Kotlin 1.3
Link copied to clipboard
open override fun nextBoolean(): Boolean

Gets the next random Boolean value.

Since Kotlin 1.3
Link copied to clipboard
open override fun nextBytes(array: ByteArray): ByteArray

Fills the specified byte array with random bytes and returns it.

Since Kotlin 1.3
open override fun nextBytes(size: Int): ByteArray

Creates a byte array of the specified size, filled with random bytes.

Since Kotlin 1.3
open override fun nextBytes(array: ByteArray, fromIndex: Int, toIndex: Int): ByteArray

Fills a subrange of the specified byte array starting from fromIndex inclusive and ending toIndex exclusive with random bytes.

Since Kotlin 1.3
Link copied to clipboard
open override fun nextDouble(): Double

Gets the next random Double value uniformly distributed between 0 (inclusive) and 1 (exclusive).

Since Kotlin 1.3
open override fun nextDouble(until: Double): Double

Gets the next random non-negative Double from the random number generator less than the specified until bound.

Since Kotlin 1.3
open override fun nextDouble(from: Double, until: Double): Double

Gets the next random Double from the random number generator in the specified range.

Since Kotlin 1.3
Link copied to clipboard
open override fun nextFloat(): Float

Gets the next random Float value uniformly distributed between 0 (inclusive) and 1 (exclusive).

Since Kotlin 1.3
Link copied to clipboard
open override fun nextInt(): Int

Gets the next random Int from the random number generator.

Since Kotlin 1.3
open override fun nextInt(until: Int): Int

Gets the next random non-negative Int from the random number generator less than the specified until bound.

Since Kotlin 1.3
open override fun nextInt(from: Int, until: Int): Int

Gets the next random Int from the random number generator in the specified range.

Since Kotlin 1.3
Link copied to clipboard
fun Random.nextInt(range: IntRange): Int

Gets the next random Int from the random number generator in the specified range.

Since Kotlin 1.3
Link copied to clipboard
open override fun nextLong(): Long

Gets the next random Long from the random number generator.

Since Kotlin 1.3
open override fun nextLong(until: Long): Long

Gets the next random non-negative Long from the random number generator less than the specified until bound.

Since Kotlin 1.3
open override fun nextLong(from: Long, until: Long): Long

Gets the next random Long from the random number generator in the specified range.

Since Kotlin 1.3
Link copied to clipboard

Gets the next random Long from the random number generator in the specified range.

Since Kotlin 1.3
Link copied to clipboard

Creates an unsigned byte array of the specified size, filled with random bytes.

Since Kotlin 1.3

Fills the specified unsigned byte array with random bytes and returns it.

Since Kotlin 1.3
fun Random.nextUBytes(array: UByteArray, fromIndex: Int = 0, toIndex: Int = array.size): UByteArray

Fills a subrange of the specified UByte array starting from fromIndex inclusive and ending toIndex exclusive with random UBytes.

Since Kotlin 1.3
Link copied to clipboard

Gets the next random UInt from the random number generator.

Since Kotlin 1.5
fun Random.nextUInt(until: UInt): UInt

Gets the next random UInt from the random number generator less than the specified until bound.

Since Kotlin 1.5

Gets the next random UInt from the random number generator in the specified range.

Since Kotlin 1.5
fun Random.nextUInt(from: UInt, until: UInt): UInt

Gets the next random UInt from the random number generator in the specified range.

Since Kotlin 1.5
Link copied to clipboard

Gets the next random ULong from the random number generator.

Since Kotlin 1.5

Gets the next random ULong from the random number generator less than the specified until bound.

Since Kotlin 1.5

Gets the next random ULong from the random number generator in the specified range.

Since Kotlin 1.5
fun Random.nextULong(from: ULong, until: ULong): ULong

Gets the next random ULong from the random number generator in the specified range.

Since Kotlin 1.5