nextInt
Gets the next random Int
from the random number generator.
Generates an Int
random value uniformly distributed between Int.MIN_VALUE
and Int.MAX_VALUE
(inclusive).
import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue
fun main(args: Array<String>) {
//sampleStart
val randomInts = List(5) { Random.nextInt() }
println(randomInts)
val sortedRandomInts = randomInts.sorted()
println(sortedRandomInts)
//sampleEnd
}
Gets the next random non-negative Int
from the random number generator less than the specified until bound.
Generates an Int
random value uniformly distributed between 0
(inclusive) and the specified until bound (exclusive).
import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue
fun main(args: Array<String>) {
//sampleStart
val menu = listOf("Omelette", "Porridge", "Cereal", "Chicken", "Pizza", "Pasta")
val forBreakfast = Random.nextInt(until = 3).let { menu[it] }
val forLunch = Random.nextInt(from = 3, until = 6).let { menu[it] }
// new meals every time
println("Today I want $forBreakfast for breakfast, and $forLunch for lunch.")
//sampleEnd
}
Parameters
Exceptions
IllegalArgumentException
- if until is negative or zero.
Gets the next random Int
from the random number generator in the specified range.
Generates an Int
random value uniformly distributed between the specified from (inclusive) and until (exclusive) bounds.
import kotlin.math.sin
import kotlin.random.Random
import kotlin.test.assertTrue
fun main(args: Array<String>) {
//sampleStart
val menu = listOf("Omelette", "Porridge", "Cereal", "Chicken", "Pizza", "Pasta")
val forBreakfast = Random.nextInt(until = 3).let { menu[it] }
val forLunch = Random.nextInt(from = 3, until = 6).let { menu[it] }
// new meals every time
println("Today I want $forBreakfast for breakfast, and $forLunch for lunch.")
//sampleEnd
}
Exceptions
IllegalArgumentException
- if from is greater than or equal to until.