import kotlin.random.Random
Random.nextInt()
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/-random/
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
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/-random/next-int.html Generates an Int random value uniformly distributed between 0 (inclusive) and the specified until bound (exclusive).
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.”)
リストをシャッフルして先頭から選んでいけば、要素が重複なく、ランダムに取り出せるだろう。 https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/shuffle.html