Skip to content

Instantly share code, notes, and snippets.

@nblackburn
Last active October 15, 2018 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nblackburn/eca349f5b4a515ba3a9d9f6676060a50 to your computer and use it in GitHub Desktop.
Save nblackburn/eca349f5b4a515ba3a9d9f6676060a50 to your computer and use it in GitHub Desktop.
Select a random item from an array of weights
module.exports = weights => {
let seed = Math.random() * weights.length;
return weights.find(weight => (seed - weight) <= 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment