diff --git a/README.md b/README.md new file mode 100644 index 0000000..4613a62 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Usage: + +`./odds.py [num_dice [sides_on_dice [monte_carlo_runs]]] [debug] [no_mc] [simple]` diff --git a/README.md b/README.md new file mode 100644 index 0000000..4613a62 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Usage: + +`./odds.py [num_dice [sides_on_dice [monte_carlo_runs]]] [debug] [no_mc] [simple]` diff --git a/odds.py b/odds.py index 6546045..6cc5c50 100755 --- a/odds.py +++ b/odds.py @@ -11,8 +11,10 @@ success_odds_by_dice = {} +sides = 6 + def get_odds(num_dice, indent = 0): - global debug, success_odds_by_dice + global sides, debug, success_odds_by_dice def iprint(val, *args): if debug: print (indent * " ") + str(val).format(*args) @@ -27,14 +29,14 @@ total_odds_success = 0 - num_rolls = pow(6, num_dice) + num_rolls = pow(sides, num_dice) iprint("num rolls with {} dice: {:,}", num_dice, num_rolls) for i in xrange(1, num_dice + 1): remaining_dice = num_dice - i - num_rolls_with_i_sixes = pow(5, remaining_dice) * fact(num_dice) / (fact(remaining_dice) * fact(i)) + num_rolls_with_i_sixes = pow(sides - 1, remaining_dice) * fact(num_dice) / (fact(remaining_dice) * fact(i)) for j in xrange(i + 1, num_dice + 1): num_rolls_with_i_sixes - iprint(" num rolls of {} dice with {} six(es): {:,}", num_dice, i, num_rolls_with_i_sixes) + iprint(" num rolls of {} dice with {} {}'s: {:,}", num_dice, i, sides, num_rolls_with_i_sixes) odds_i_sixes = num_rolls_with_i_sixes / float(num_rolls) iprint(" odds of that happening: {}", odds_i_sixes) total_odds_success += get_odds(num_dice - i, indent + 3) * odds_i_sixes @@ -52,7 +54,7 @@ while dice_left > 0: num_sixes = 0 for j in xrange(0, dice_left): - if random.randint(1, 6) == 6: + if random.randint(1, sides) == sides: num_sixes += 1 if num_sixes == 0: dice_left = 0 @@ -75,21 +77,26 @@ do_monte_carlo = not simple_output and not find_flag("no_mc") dice = int(sys.argv[1]) -odds = get_odds(dice) -monte_carlo_iters = 100000 + if len(sys.argv) > 2: - monte_carlo_iters = int(sys.argv[2]) + sides = int(sys.argv[2]) + +monte_carlo_iters = 100000 +if len(sys.argv) > 3: + monte_carlo_iters = int(sys.argv[3]) if do_monte_carlo: monte_carlo_odds = get_monte_carlo_odds(monte_carlo_iters, dice) +odds = get_odds(dice) + if debug: # extra line to separate debug output print if simple_output: print odds else: - odds_str = "odds of getting donuts with {} dice: {:.2}%".format(dice, odds * 100) + odds_str = "odds of getting donuts with {} {}-sided dice: {:.2}%".format(dice, sides, odds * 100) if do_monte_carlo: odds_str += ", monte carlo with {:,} iterations gives: {:.2}%".format(monte_carlo_iters, monte_carlo_odds * 100) print odds_str