Created
April 4, 2015 16:13
-
-
Save mccutchen/d0a624ebff6ed82bf768 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def weighted_choice(choices): | |
| # http://stackoverflow.com/a/3679747/151221 | |
| total = sum(w for c, w in choices) | |
| r = random.uniform(0, total) | |
| for c, w in choices: | |
| r -= w | |
| if r <= 0: | |
| return c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment