Last active
August 29, 2015 14:07
-
-
Save jbm9/4662d56a6739047ef3aa to your computer and use it in GitHub Desktop.
Makes arbitrary decisions for you
This file contains hidden or 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: | |
# decide.py ==> yes/no, 50/50 | |
# decide.py 0.1 ==> gives a 10% chance of yes | |
# decide.py foo bar 3xbaz ==> gives a choice from foo bar baz baz baz | |
# | |
import sys | |
import random | |
args = sys.argv[1:] | |
if len(args) == 0: | |
args = [ "yes", "no" ] | |
if len(args) == 1: | |
p = float(args[0]) | |
r = random.random() | |
print "%f<=>%f => %s" % (r,p, "Yes" if r <= p else "No") | |
sys.exit(0) | |
def parse_arg(s): | |
try: | |
a,b = s.split("x",1) | |
n = int(a) | |
except: | |
return [s] | |
return [b] * n | |
choices = [] | |
for a in args: | |
choices += parse_arg(a) | |
result = random.sample(choices, 1) | |
print "Choices: %s" % str(choices) | |
print result[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment