# compute probabilities for a game in which two players roll six-sided dice and the player with the # larger sum wins; output: # - number of dice rolled by player 1 # - number of dice rolled by player 2 # - probability of player 1 win # - probability of draw # - probability of player 2 win import collections, itertools # number of dice rolled by player 1 for dieCnt1 in range(1, 7): # key = sum of player 1's dice, value = how many ways to get it scores1 = collections.Counter( sum(dice) for dice in itertools.product(range(1, 7), repeat=dieCnt1) ) # number of dice rolled by player 2 for dieCnt2 in range(dieCnt1, 7): # key = sum of player 2's dice, value = how many ways to get it scores2 = collections.Counter( sum(dice) for dice in itertools.product(range(1, 7), repeat=dieCnt2) ) # total number of possible results totalCnt = sum(scores1.values()) * sum(scores2.values()) # count player 1 wins, player 2 wins and draws; e.g. for each combination of sums that's # a win for player 1, weight it by the number of ways it can be achieved winCnt1 = sum(sum(scores2[b] for b in scores2 if a > b) * scores1[a] for a in scores1) winCnt2 = sum(sum(scores2[b] for b in scores2 if a < b) * scores1[a] for a in scores1) drawCnt = totalCnt - winCnt1 - winCnt2 fields = [str(c) for c in (dieCnt1, dieCnt2)] fields.extend(f"{c/totalCnt:.2%}" for c in (winCnt1, drawCnt, winCnt2)) print(" " + "".join(fields) + "")