Skip to main content
The Deno 2 Release Candidate is here
Learn more
method WeightedRandomHelper.prototype.weightedRandom
Re-export
import { WeightedRandomHelper } from "https://deno.land/x/sptaki@1.2.0/helpers/mod.ts";

Picks the random item based on its weight. The items with higher weight will be picked more often (with a higher probability).

For example:

  • items = ['banana', 'orange', 'apple']
  • weights = [0, 0.2, 0.8]
  • weightedRandom(items, weights) in 80% of cases will return 'apple', in 20% of cases will return 'orange' and it will never return 'banana' (because probability of picking the banana is 0%)

Parameters

items: any[]
weights: any[]

Returns

{ item: any; index: number; }