W3cubDocs

/Kotlin

Default

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
companion object Default : Random

The default random number generator.

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

import kotlin.random.Random
import kotlin.test.assertTrue

fun main(args: Array<String>) {
//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
}

Companion Object Functions

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

nextBits

Gets the next random bitCount number of bits.

fun nextBits(bitCount: Int): Int
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

nextBoolean

Gets the next random Boolean value.

fun nextBoolean(): Boolean
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

nextBytes

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

fun nextBytes(array: ByteArray): ByteArray

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

fun nextBytes(size: Int): ByteArray

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

fun nextBytes(
    array: ByteArray, 
    fromIndex: Int, 
    toIndex: Int
): ByteArray
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

nextDouble

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

fun nextDouble(): Double

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

fun nextDouble(until: Double): Double

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

fun nextDouble(from: Double, until: Double): Double
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

nextFloat

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

fun nextFloat(): Float
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

nextInt

Gets the next random Int from the random number generator.

fun nextInt(): Int

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

fun nextInt(until: Int): Int

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

fun nextInt(from: Int, until: Int): Int
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

nextLong

Gets the next random Long from the random number generator.

fun nextLong(): Long

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

fun nextLong(until: Long): Long

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

fun nextLong(from: Long, until: Long): Long

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/-random/-default/index.html