Skip to content

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.

RandomSource

readonly number[]

number

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.

With code INVALID_SOURCE_VALUE when the random source returns a value outside [0, 1).