Skip to the content

Tane種

Reproduce numbers from a seed, shuffle or pick items, share a daily puzzle and resume a saved stream.

Tane means seed in Japanese. About the name

Contents

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

NameTypeDescription
randomRandom

A random source returning values in [0, 1).

itemsreadonly T[]

The items to choose or shuffle; shuffle mutates this array, while shuffled copies it.

weightsreadonly 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]));
high

What it prints (run when this page was built)

View sourceEdit on GitHubMade from the source of @johnmorrisdotca/tane 1.3.0.