Last active
October 15, 2018 14:41
-
-
Save nblackburn/eca349f5b4a515ba3a9d9f6676060a50 to your computer and use it in GitHub Desktop.
Select a random item from an array of weights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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