Search code examples
pythonalgorithmpython-itertoolspython-collections

itertools dice rolls: doubles roll twice


I'm trying to learn the Python library itertools and I thought a good test would be the simulation of dice rolls. It's easy to generate all possible rolls using product and counting the number of possible ways of doing so with the collections library. I'm trying to solve the problem that comes up in games like Monopoly: when doubles are rolled, you roll again and your final total is the sum of the two rolls.

Below is my starting attempt at solving the problem: two Counters, one for doubles and the other for not doubles. I'm not sure if there is a good way to combine them or if the two Counters are even the best way of doing it.

I'm looking for a slick way of solving (by enumeration) the dice roll problem with doubles using itertools and collections.

import numpy as np
from collections import Counter
from itertools import *

die_n = 2
max_num = 6

die = np.arange(1,max_num+1)
C0,C1  = Counter(), Counter()

for roll in product(die,repeat=die_n):
    if len(set(roll)) > 1: C0[sum(roll)] += 1
    else: C1[sum(roll)] += 1

Solution

  • Leaving out numpy here for the sake of simplicity:

    First, generate all rolls, be it single or double rolls:

    from itertools import product
    from collections import Counter
    
    def enumerate_rolls(die_n=2, max_num=6):
        for roll in product(range(1, max_num + 1), repeat=die_n):
            if len(set(roll)) != 1:
                yield roll
            else:
                for second_roll in product(range(1, max_num + 1), repeat=die_n):
                    yield roll + second_roll
    

    Now a few tests:

    print(len(list(enumerate_rolls()))) # 36 + 6 * 36 - 6 = 246
    A = list(enumerate_rolls(5, 4))
    print(len(A)) # 4 ** 5 + 4 * 4 ** 5 - 4 = 5116
    print(A[1020:1030]) # some double rolls (of five dice each!) and some single rolls
    

    and the result:

    246
    5116
    [(1, 1, 1, 1, 1, 4, 4, 4, 4, 1), (1, 1, 1, 1, 1, 4, 4, 4, 4, 2), (1, 1, 1, 1, 1, 4, 4, 4, 4, 3), (1, 1, 1, 1, 1, 4, 4, 4, 4, 4), (1, 1, 1, 1, 2), (1, 1, 1, 1, 3), (1, 1, 1, 1, 4), (1, 1, 1, 2, 1), (1, 1, 1, 2, 2), (1, 1, 1, 2, 3)]
    

    To get the totals use the special Counter capabilities:

    def total_counts(die_n=2, max_num=6):
        return Counter(map(sum, enumerate_rolls(die_n, max_num)))
    
    print(total_counts())
    print(total_counts(5, 4))
    

    Results:

    Counter({11: 18, 13: 18, 14: 18, 15: 18, 12: 17, 16: 17, 9: 16, 10: 16, 17: 16, 18: 14, 8: 13, 7: 12, 19: 12, 20: 9, 6: 8, 5: 6, 21: 6, 22: 4, 4: 3, 3: 2, 23: 2, 24: 1})
    Counter({16: 205, 17: 205, 18: 205, 19: 205, 21: 205, 22: 205, 23: 205, 24: 205, 26: 205, 27: 205, 28: 205, 29: 205, 25: 204, 20: 203, 30: 203, 15: 202, 14: 200, 31: 200, 13: 190, 32: 190, 12: 170, 33: 170, 11: 140, 34: 140, 35: 102, 10: 101, 9: 65, 36: 65, 8: 35, 37: 35, 7: 15, 38: 15, 6: 5, 39: 5, 40: 1})
    

    Note: At this point, there is no way of computing the probability for the totals. You have to know if it is a double roll or a total roll to weigh correctly.