Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2015 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/3a92505aac1d4b1c0a39 to your computer and use it in GitHub Desktop.
Save anonymous/3a92505aac1d4b1c0a39 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