Reference · Streams, draws and seeds
weightedPickfunction
One item, chosen by the matching weight: one draw.
Import it
import { weightedPick } from "@johnmorrisdotca/tane";
Definition
function weightedPick<T>(random: Random, items: readonly T[], weights: readonly number[]): T
Parameters
| Name | Type | Description |
|---|---|---|
random | Random | A random source returning values in [0, 1). |
items | readonly T[] | The items to choose or shuffle; shuffle mutates this array, while shuffled copies it. |
weights | readonly number[] | Nonnegative weights in the same order as the corresponding items. |
Returns
T
One item, chosen by the matching weight: one draw.
Examples
import { mulberry32, weightedPick } from "@johnmorrisdotca/tane";
const random = mulberry32(42);
console.log(weightedPick(random, ["low", "high"], [1, 3]));highWhat it prints (run when this page was built)