sampleOrNull

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

Returns n elements sampled without replacement, or null when n is out of range.

Behaves like sample for valid arguments and returns null instead of throwing when n is negative or greater than the collection size.

Parameters

n

the number of elements to sample.

random

the source of randomness, Random.Default by default.