-
-
Save biesnecker/4e2b3c857e5d6eebf846 to your computer and use it in GitHub Desktop.
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
#!/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