Created
February 6, 2013 13:00
-
-
Save psycharo-zz/4722377 to your computer and use it in GitHub Desktop.
gets a randomly weighted item from list
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
def get_random_item(weights): | |
num = random.uniform(0, 1) | |
for i, w in enumerate(weights): | |
if w > num: | |
return i | |
num -= w |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment