Skip to content

Instantly share code, notes, and snippets.

@psycharo-zz
Created February 6, 2013 13:00
Show Gist options
  • Save psycharo-zz/4722377 to your computer and use it in GitHub Desktop.
Save psycharo-zz/4722377 to your computer and use it in GitHub Desktop.
gets a randomly weighted item from list
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