themer/cli/packages/wallpaper-block-wave/lib/weighted-random.js
2020-04-25 13:53:31 -06:00

6 lines
316 B
JavaScript

module.exports = map => {
const cumulativeWeights = Array.from(map.values()).reduce((c, weight, i) => c.concat(weight + (i === 0 ? 0 : c[i-1])), []);
const random = cumulativeWeights[cumulativeWeights.length-1] * Math.random();
return Array.from(map.keys())[cumulativeWeights.findIndex(cw => random < cw)];
};