Skip to content

Instantly share code, notes, and snippets.

@bickfordb
Created July 15, 2012 04:33
Show Gist options
  • Save bickfordb/3115004 to your computer and use it in GitHub Desktop.
Save bickfordb/3115004 to your computer and use it in GitHub Desktop.
choice
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