# weightedSampleFromLogWeights

> **weightedSampleFromLogWeights**(`random`, `logWeights`, `sampleSize`): readonly `number`\[]

Defined in: [packages/sampling/src/weighted-sample-from-log-weights.ts:30](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/sampling/src/weighted-sample-from-log-weights.ts#L30)

Sample indices without replacement from log-weights, in ascending source order.

Each entry is the natural logarithm of a non-negative weight, so `weightedSampleFromLogWeights(random, weights.map(Math.log), sampleSize)` returns exactly what `weightedSample` returns for the same finite weights and source state. Skipping the round trip through `Math.exp` keeps an entry eligible and separable however small its weight is: a log-weight of `-800` is a valid input even though `Math.exp(-800)` is zero.

`-Infinity` marks an ineligible entry, mirroring a zero weight exactly: it is skipped before its draw and can never be returned. Every other eligible entry consumes exactly one draw, in source order, and a `sampleSize` of zero consumes none. Uses Efraimidis-Spirakis keys and a bounded min priority queue, taking O(logWeights.length log sampleSize) time and O(sampleSize) auxiliary space. Equal keys prefer the lower source index, and the log-weights are not mutated.

## Parameters

### random

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

### logWeights

readonly `number`\[]

### sampleSize

`number`

## Returns

readonly `number`\[]

## Throws

With code `INVALID_LOG_WEIGHT` when a log-weight is `NaN` or `+Infinity`, `INVALID_SAMPLE_SIZE` when `sampleSize` is not a valid array size, or `INSUFFICIENT_POSITIVE_WEIGHT` when too few log-weights are finite.

## Throws

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