Skip to content
party.js logoparty.js logo

ReferenceSamplersFunction

pick()

Picks one of several values at random.

function pick<T>(arr: T[], weights?: number[]): SamplerFn<T>;

Creates a sampler that picks one of the given values at random, either with equal chances or weighted ones.

Weights are relative and don’t need to add up to 1: [3, 1] will make the first value three times as likely as the second one.

T
arrT[]

The values to pick from.

weightsoptionalnumber[]

Optional relative chances, one per value.

PartyJSError

If arr is empty, if the number of weights doesn’t match the number of values, if a weight is negative or if all weights are zero.

// Mostly circles with an occasional star.
pick([{ type: "circle" }, { type: "star" }], [4, 1]);