Throwing dice.

Welcome back my friend!

With this post I want to talk about some interesting applications of the theory of generating functions in relation to the enumeration of combinations.

For this purpose let’s introduce a very simple problem: “Say we throw three regular dice, in how many ways the sum of the upper faces can be equal to 15?” That sounds like a reasonably simple problem for which we will develop a set of combinatorics tools, don’t be scared by the math, is really simple and at the end of the post we will be able to extend this exercise to dice and any valid resulting sum. The theory behind the calculations here performed has some extremely important implications in the theory of coding and in general might be extended to a wider set of problems, but for now let’s start with the three dice..

So, we want to have d_{1}+d_{2}+d_{3}=15 where 1 \leq d_{i}\leq 6, that make perfectly sense right? Regular dice have six faces and we want the sum of the faces to be 15. For the moment let’s start with a much simpler enumeration problem which will give us some insight on how to proceed: “In how many ways can we select three distinguishable objects a,b and c?”

For every possible combination we have to decide whether we want to take or not take a, then perform the same decision for b and c. We can display this dilemma in mathematical language by building the schematic product for our decision problem: We can either pick no a or one a, we cannot pick more than one a, the same logic apply for b and c, we either pick nothing ore one.

This mean that can build a sequence: a_{k}=\left(0,1\right),b_{k}=\left(0,1\right) and c_{k}=\left(0,1\right). In general for any given w_{k}=\left(a_{0},a_{1}...\right), we have a generating function G\left(x\right)=\sum_{k=0}^{\infty} a_{k}x^{k}=a_{0}x^{0}+a_{1}x^{1}+a_{2}x^{2} \cdots which in our simple case is just a_{0}x^{0}+a_{1} x^{1}b_{0}x^{0}+b_{1}x^{1} and c_{0}x^{0}+c_{1}x^{1}, that is, only two terms,

The schematic product for our selection problem is given by the expression (Where the sum is an OR operation and the multiplication an AND operation):


Which have the meaning of: one can either pick zero a or one a and zero b or one b &c, after the obvious simplification for the first coefficient (the zero coefficient), the expression can be expanded to:


Where the coefficients are giving us the ways to select the three distinguishable objects a,b and c, including the empty selection which is represented by that lonely 1. Now, if you put a=b=c=1 you get the number of selections:


Which is what we’re looking for, the coefficients of the expression are telling us in how many ways we can pick the three objects: Three ways of picking one object, three ways of picking two objects and one way to pick them all..

That’s great, let’s come back to our original problem since now we have one important tool to solve it. A dice have six faces, each face have a value 1\leq d_{i}\leq 6 and we have three dice, so our schematic product is:


note that the term for the zero coefficient is missing, that is because we cannot have zero as possible result for a dice roll, the exponent three is due to the amount of dice we have, that is clearly the multiplication of three generating functions which are modelling our dice.

What we need to solve our problem is the coefficient of x^{15}, remember this is the same setup as for the simple a,b,c problem, we want the amount of possible combinations which give us 15. To find this coefficient we do not need to expand that expression, I mean we can, but that’s a tedious and error prone exercise and for sure is not possible for bigger problems with 100 dice for example (is possible, but stupid).

We need to reformulate that expression in a way which allow us to perform some manipulation, let’s start with some series:



In our case, we have:


Which can be further simplified to:


Very well, we’re almost done! From this expression we need to find the coefficient c_{15}x^{15}. The only problematic part seems to be \left(\frac{1}{1-x}\right)^{3} or more generally \left(\frac{1}{1-x}\right)^{p}, if you have a closer look you can spot that the the Maclaurin series expansion for \frac{1}{1-x} is:

\frac{1}{1-x}=\sum_{k=0}^{\infty}x^{k}=1+x^{1}+x^{2}+\cdots, for \left | x \right | < 1

which would be very helpful if there wasn’t any exponent to that fraction, to find the coefficient we’re looking for one can use the binomial theorem \left( 1+x \right)^{n}=\sum_{r=0}^{\infty}\binom{n}{r}x^{r} putting -x instead of x and -p instead of n we have that the coefficient of x^{k} is given by \binom{-p}{k}\left ( -1 \right )^{k}, which after some manipulations ends up to be equal to \binom{k+p-1}{k}.

That’s not a coincidence that the coefficient c_{k} of \left (1-x\right)^{-p} is equal to the amount of ways to place k indistinguishable balls into p distinguishable cells, but for the moment let’s go back to our original question, how to find the coefficient of x^{15}.

Our expression has three parts which are multiplied together, each part has a certain amount of terms which have the form of c_{i}x^{n} from \left (1-x^{6}\right)^{3}, and g_{j}x^{m} from \left (1-x\right )^{-3} and the obvious x^{3} from guess what… x^{3}

We need to fulfill this condition:

x^{3}c_{i}x^{n}g_{j}x^{m}=c_{r}x^{15} where 15=m+n+3

applying the binomial theorem we can find the coefficients c_{i}=\left (\binom{3}{0},-\binom{3}{1},\binom{3}{2},...\right ) for x^{0},x^{1},\cdots and using \binom{k+p-1}{k} the coefficients of g_{j}, so we can put all together and see from what that 15 can be made of:

  1. x^{3}\binom{3}{0}c_{12}x^{12}=91x^{15}
  2. x^{3}\left (-\binom{3}{1}\right )c_{6}x^{6}=-84x^{15}
  3. x^{3}\binom{3}{2}c_{0}x^{0}=3x^{15}

For which we have: 91x^{15}-84x^{15}+3x^{15}=10x^{15} ! That’s done, the solution for our problem is 10There are 10 ways to get 15 from the sum of the upper faces of three rolled dice, we can verify this by expanding our original expression (or by rolling 3 dice).

Let’s see how to use this knowledge to solve larger problems, we can use the exact same procedure developed here to calculate any valid sum t for any number n of dice:

using myint = int_fast32_t;
myint number_of_sums( myint n, myint t )
    myint solution{ 0 };
    myint c1{ 0 }; //This is the coefficient of (1-x^6)^n
    myint c2{ 0 }; //Coefficient of (1-x)^(-n)
    for( myint i{ 0 }, k{ 0 }; k + n <= t ; k += 6, ++i )
        c1 = pascal[ n ][ i ];
        if( 0 != i%2 ) {
            c1 *= -1;
        c2 = pascal[ t - k - 1 ][ t - n - k ];
        solution += c1 * c2;
    return solution;

Where pascal is the pascal’s triangle, which can be build with some simple method like this:

constexpr myint max_size{ 1000 };
myint pascal[ max_size ][ max_size ];
void calculate_pascal()
    for( myint i{ 0 } ; i < max_size ; ++i )
        pascal[ i ][ 0 ] = 1;
        pascal[ i ][ i ] = 1;
    for( myint n{ 2 } ; n < max_size ; ++n )
        for( myint k{ 1 } ; k < n ; ++k )
            pascal[ n ][ k ] = pascal[ n-1 ][ k-1 ] + pascal[ n-1 ][ k ];

and now we calculate the amount of ways to obtain a sum of 250 when rolling 100 dice: 5.270.981.265.418.279.600! Cool!

Thanks for reading, hope you had fun!

Leave a Reply