weightedRandom

fun <T> List<T>.weightedRandom(weights: List<Double>, random: Random = Random.Default): T

Returns a random element of this list chosen with probability proportional to weights.

The probability of element i equals weights[i] / weights.sum(). Elements whose weight is 0.0 are never chosen.

Parameters

weights

one non-negative finite weight per element of this list.

random

the source of randomness, Random.Default by default.

Throws

if the list is empty, if weights and the list differ in size, if any weight is negative, NaN, or infinite, or if the total weight is not strictly positive.