weightedChoice(
random,weights):number
Defined in: packages/sampling/src/weighted-choice.ts:17
Sample one index proportionally to its finite non-negative weight.
Runs in O(weights.length) time and O(1) auxiliary space. Scaling by the largest weight keeps the cumulative sum finite without changing the distribution. The weights are not mutated.
Parameters
Section titled “Parameters”random
Section titled “random”weights
Section titled “weights”readonly number[]
Returns
Section titled “Returns”number
Throws
Section titled “Throws”With code INVALID_WEIGHT when a weight
is negative or non-finite, or ZERO_TOTAL_WEIGHT when the array is empty or
contains no positive weight.
Throws
Section titled “Throws”With code INVALID_SOURCE_VALUE when the
random source returns a value outside [0, 1).