Skip to content

Instantly share code, notes, and snippets.

@oepn
oepn / random-weighted-es6.js
Last active December 28, 2020 21:16 — forked from aesnyder/random-weighted.coffee
Weighted random value in ES6
// Defined first due to TDZ
let weighted = (...weightMap) => weightMap
.map(({0: value, 1: weight}) => new Array(weight).fill(value))
.reduce((acc, current) => [...acc, ...current]);
let random = (array) => array[Math.floor(Math.random() * array.length)];
// You can generate a weighted array