import sys import json import pprint pp = pp.Printer = pprint.PrettyPrinter(indent=2, compact=True) def usagequit(): print("""Usage: python pydiceprob.py [k] [mode] k between 6 and 99 modes: single, single-table, multi, multi-table single prints out the probability of a single throw with a k-sided dice yielding k. single-table prints out a table between 6 and k with the probabilities of a single throw yielding k. multi prints out the probability of winning (assuming we're going first) in a game where two players take turns and the person who throws k first wins. multi-table prints out the probability of winning for a range between 6 and k if you have the first throw.""") quit() def main(): global k try: if int(sys.argv[1]) >= 6: k = int(sys.argv[1]) else: usagequit() except IndexError: usagequit() global mode try: mode = sys.argv[2] except IndexError: usagequit() dispatch(mode, k) def dispatch(mode, k): modes = ["single", "multi", "single-table", "multi-table"] if mode not in modes: usagequit() if mode == "single": print(first_turn_probability(k)) elif mode == "multi" : print(multi_turn_single(k)) elif mode == "single-table": print(first_turn_protatbilities(k)) elif mode == "multi-table": print(multi_turn_table(k)) def dice(k): if k < 6: print("Dice must be at least 6-sided") elif k == 6: dice_array = [6] else: dice_array = list(range(6, k)) def first_turn_probabilities(k): result = {} for k in range(6, int(k)+1): result[k] = 1/k return result def first_turn_probability(k): return first_turn_probabilities(k)[k] def multi_turn_single(k): p_win = 1 / k # Winning probability on any given throw p_lose = (k-1) / k # Losing probability on any given throw bob_wins_prob_sum = 0 r = p_lose**2 probability_win = p_win / (1 - r) return probability_win def multi_turn_table(k): result = {} for i in range(6, int(k+1)): result[i] = multi_turn_single(i) return result def print(stuff): pp.pprint(stuff) if __name__ == "__main__": main()