Skip to content

Instantly share code, notes, and snippets.

@biesnecker
Forked from anonymous/choices.py
Last active August 29, 2015 14:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save biesnecker/4e2b3c857e5d6eebf846 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# usage: choices choice_a 1 choice_b 5 choice_c 4
# will return choice_a 10% of the time, choice_b 50% of the time
# and choice_c 40% of the time
from random import randrange
import sys
options = []
total = 0
args = sys.argv[1:]
if args and len(args) % 2 == 0:
while args:
options.append((args[0], int(args[1])))
total += int(args[1])
args = args[2:]
n = randrange(0, total)
for option in options:
n = n - option[1]
if n < 0:
print(option[0])
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment