Default
companion object Default : RandomThe 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 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): Long