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

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

NameTypeDescription
randomRandom

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

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

What it prints (run when this page was built)

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