# weightedSample

> **weightedSample**(`random`, `weights`, `sampleSize`): readonly `number`\[]

Defined in: [packages/sampling/src/weighted-sample.ts:21](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/sampling/src/weighted-sample.ts#L21)

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

### random

[`RandomSource`](/reference/knotkit/prng/type-aliases/randomsource/)

### weights

readonly `number`\[]

### sampleSize

`number`

## Returns

readonly `number`\[]

## 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

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