Created
July 15, 2012 04:33
-
-
Save bickfordb/3115004 to your computer and use it in GitHub Desktop.
choice
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 choose1(weighted_elements): | |
total = 0 | |
selected = weighted_elements[0][0] | |
for item, weight in weighted_elements: | |
total += weight | |
k = random.random() * total | |
if k <= weight: | |
selected = item | |
return selected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment