Random
An 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.
Since Kotlin
1.3Samples
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
}
Inheritors
Functions
Creates a java.util.Random instance that uses the specified Kotlin Random generator as a randomness source.
Fills the specified byte array with random bytes and returns it.
Creates a byte array of the specified size, filled with random bytes.
Fills a subrange of the specified byte array starting from fromIndex inclusive and ending toIndex exclusive with random bytes.
Gets the next random Double value uniformly distributed between 0 (inclusive) and 1 (exclusive).
Gets the next random non-negative Double
from the random number generator less than the specified until bound.
Gets the next random Double
from the random number generator in the specified range.
Gets the next random Int
from the random number generator.
Gets the next random non-negative Int
from the random number generator less than the specified until bound.
Gets the next random Int
from the random number generator in the specified range.
Gets the next random Long
from the random number generator.
Gets the next random non-negative Long
from the random number generator less than the specified until bound.
Gets the next random Long
from the random number generator in the specified range.
Creates an unsigned byte array of the specified size, filled with random bytes.
Fills the specified unsigned byte array with random bytes and returns it.
Fills a subrange of the specified UByte
array starting from fromIndex inclusive and ending toIndex exclusive with random UBytes.