weightedSample(
random,weights,sampleSize): readonlynumber[]
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.
Parameters
Section titled “Parameters”random
Section titled “random”weights
Section titled “weights”readonly number[]
sampleSize
Section titled “sampleSize”number
Returns
Section titled “Returns”readonly number[]
Throws
Section titled “Throws”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.
Throws
Section titled “Throws”With code INVALID_SOURCE_VALUE when the
random source returns a value outside [0, 1).