Reference · Streams, draws and seeds
weightedIndexfunction
An index into weights, each chosen in proportion to its weight: one draw.
Weights must not be negative; a list whose weights sum to nothing gives 0.
Import it
import { weightedIndex } from "@johnmorrisdotca/tane";
Definition
function weightedIndex(random: Random, weights: readonly number[]): number
Parameters
| Name | Type | Description |
|---|---|---|
random | Random | A random source returning values in [0, 1). |
weights | readonly number[] | Nonnegative weights in the same order as the corresponding items. |
Returns
number
An index into weights, each chosen in proportion to its weight: one draw. Weights must not be negative; a list whose weights sum to nothing gives 0.
Examples
import { mulberry32, weightedIndex } from "@johnmorrisdotca/tane";
const random = mulberry32(42);
console.log(weightedIndex(random, [1, 3]));1What it prints (run when this page was built)