Reference · Streams, draws and seeds
samplefunction
count items of a list, no item twice, in the order drawn: a partial
Fisher–Yates, one draw each. Asking for more than the list holds gives the
whole list, shuffled.
Import it
import { sample } from "@johnmorrisdotca/tane";
Definition
function sample<T>(random: Random, items: readonly T[], count: 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. |
count | number | The number of items or draws requested. |
Returns
T[]
count items of a list, no item twice, in the order drawn: a partial Fisher–Yates, one draw each. Asking for more than the list holds gives the whole list, shuffled.
Examples
import { mulberry32, sample } from "@johnmorrisdotca/tane";
const random = mulberry32(42);
console.log(sample(random, ["A", "B", "C", "D"], 3));[ 'C', 'A', 'D' ]What it prints (run when this page was built)