Random
abstract class RandomAn abstract class that is implemented by random number generator algorithms.
The companion object Random.Default is the default instance of Random.
To get a seeded instance of random generator use Random function.
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
}Types
Constructors
<init>
An abstract class that is implemented by random number generator algorithms.
Random()Functions
nextBoolean
Gets the next random Boolean value.
open fun nextBoolean(): BooleannextBytes
Fills a subrange of the specified byte array starting from fromIndex inclusive and ending toIndex exclusive with random bytes.
Fills the specified byte array with random bytes and returns it.
nextDouble
Gets the next random Double value uniformly distributed between 0 (inclusive) and 1 (exclusive).
open fun nextDouble(): DoubleGets the next random non-negative Double from the random number generator less than the specified until bound.
open fun nextDouble(until: Double): DoubleGets the next random Double from the random number generator in the specified range.
open fun nextDouble(from: Double, until: Double): DoublenextFloat
Gets the next random Float value uniformly distributed between 0 (inclusive) and 1 (exclusive).
open fun nextFloat(): FloatnextInt
Gets the next random Int from the random number generator.
open fun nextInt(): IntGets the next random non-negative Int from the random number generator less than the specified until bound.
open fun nextInt(until: Int): IntGets the next random Int from the random number generator in the specified range.
open fun nextInt(from: Int, until: Int): IntnextLong
Gets the next random Long from the random number generator.
open fun nextLong(): LongGets the next random non-negative Long from the random number generator less than the specified until bound.
open fun nextLong(until: Long): LongGets the next random Long from the random number generator in the specified range.
open fun nextLong(from: Long, until: Long): LongCompanion Object Properties
Companion
val Companion: CompanionCompanion Object Functions
nextBoolean
Gets the next random Boolean value.
fun nextBoolean(): BooleannextBytes
Fills the specified byte array with random bytes and returns it.
nextDouble
Gets the next random Double value uniformly distributed between 0 (inclusive) and 1 (exclusive).
fun nextDouble(): DoubleGets the next random non-negative Double from the random number generator less than the specified until bound.
fun nextDouble(until: Double): DoubleGets the next random Double from the random number generator in the specified range.
fun nextDouble(from: Double, until: Double): DoublenextFloat
Gets the next random Float value uniformly distributed between 0 (inclusive) and 1 (exclusive).
fun nextFloat(): FloatnextInt
Gets the next random Int from the random number generator.
fun nextInt(): IntGets the next random non-negative Int from the random number generator less than the specified until bound.
fun nextInt(until: Int): IntGets the next random Int from the random number generator in the specified range.
fun nextInt(from: Int, until: Int): IntnextLong
Gets the next random Long from the random number generator.
fun nextLong(): LongGets the next random non-negative Long from the random number generator less than the specified until bound.
fun nextLong(until: Long): LongGets the next random Long from the random number generator in the specified range.
fun nextLong(from: Long, until: Long): LongExtension Functions
asJavaRandom
Creates a java.util.Random instance that uses the specified Kotlin Random generator as a randomness source.
nextUBytes
Fills the specified unsigned byte array with random bytes and returns it.
fun Random.nextUBytes(array: UByteArray): UByteArrayCreates an unsigned byte array of the specified size, filled with random bytes.
fun Random.nextUBytes(size: Int): UByteArrayFills a subrange of the specified UByte array starting from fromIndex inclusive and ending toIndex exclusive with random UBytes.
fun Random.nextUBytes(
array: UByteArray,
fromIndex: Int = 0,
toIndex: Int = array.size
): UByteArraynextUInt
Gets the next random UInt from the random number generator in the specified range.
nextULong
Gets the next random ULong from the random number generator in the specified range.
Gets the next random ULong from the random number generator in the specified range.
fun Random.nextULong(range: ULongRange): ULong