Skip to content

weightedSample(random, weights, sampleSize): readonly number[]

Defined in: packages/sampling/src/weighted-sample.ts:21

Sample weighted indices without replacement in ascending source order.

Uses Efraimidis-Spirakis keys ranked in log space and a bounded min priority queue, taking O(weights.length log sampleSize) time and O(sampleSize) auxiliary space. Zero-weight entries are ineligible, equal keys prefer the lower source index, and the weights are not mutated. Ranking in log space keeps subnormal weights separable rather than tying them all together.

RandomSource

readonly number[]

number

readonly number[]

With code INVALID_WEIGHT when a weight is negative or non-finite, INVALID_SAMPLE_SIZE when sampleSize is not a valid array size, or INSUFFICIENT_POSITIVE_WEIGHT when too few weights are positive.

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