sampleWithReplacement

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

Returns n elements sampled uniformly at random with replacement.

Each element is chosen independently, so the same position can be picked multiple times and n may exceed the collection size. An n of zero yields the empty list even for an empty collection.

Parameters

n

the number of elements to sample, must be non-negative.

random

the source of randomness, Random.Default by default.

Throws

if n is negative, or if the collection is empty while n is positive.