sample

fun <T> Collection<T>.sample(n: Int, random: Random = Random.Default): List<T>

Returns n elements sampled uniformly at random without replacement.

Each element position of this collection is used at most once, so the result has no repeated positions; repeated values can still appear when the source itself contains duplicates. Every subset of size n is equally likely. The relative order of the result is randomized.

Parameters

n

the number of elements to sample, must be within 0..size.

random

the source of randomness, Random.Default by default.

Throws

if n is negative or greater than the collection size.