choice

@JvmName(name = "choiceVararg")
fun <T> Random.choice(vararg elements: T): T

Returns a random element from this array using the specified source of randomness.

Example:

Random.choice(1, 2, 3, 4, 5) // a number in the range 1..5

Since

1.1.0

See also

random

Throws

NoSuchElementException
  • if this array is empty.


fun <T> Random.choice(array: Array<T>): T

Returns a random element from this array using the specified source of randomness.

Example:

Random.choice(arrayOf(1..5)) // a number in the range 1..5

Since

1.1.0

See also

random

Throws

NoSuchElementException
  • if this array is empty.


fun <T> Random.choice(collection: Collection<T>): T

Returns a random element from this collection using the specified source of randomness.

Example:

Random.choice(listOf(1..5)) // a number in the range 1..5

Since

1.1.0

See also

random

Throws

NoSuchElementException
  • if this collection is empty.


inline fun <T, K> Random.choice(map: Map<T, K>): Pair<T, K>

Returns a random element from this map using the specified source of randomness.

Example:

Random.choice(mapOf(1 to 1, 2 to 2, 3 to 3, 4 to 4, 5 to 5)) // a pair from this map

Since

1.1.0

Throws

NoSuchElementException
  • if this map is empty.