Sums of dice throws

My little engineering students are studying a bit of probability as part of their first year mathematics, and their textbook is full of questions like “If two fair dice are thrown, what is the probability that the sum is at least 5?”. Like almost every probability textbook written since the dawn of time, it will include a table such as this:

or a picture such as this:

illustrating all possible throws of two dice. From either of these the number of ways of obtaining a sum is given by:

sum: 2   3   4   5   6   7   8   9  10  11  12
  #: 1   2   3   4   5   6   5   4   3   2   1

From a table such as this it is easy to answer all probability questions from the book. Very few books, however, go into the question about sums from three or more dice. I imagine that there is little further learning of probability to be gained by thus extending the problem, so for multiple “throws” coins are used rather than dice.

However, the mathematics is quite fun. Suppose k (6-sided, fair) dice are thrown. In how many ways can the sum n be obtained? Let’s call this number t(k,n). From the table above, you can see that, for example t(2,9)=5. But what is the value, for example, or t(3,10), or of t(100,300)?

Look at the case k=3. We are interested in the number of sums n_1+n_2+n_3=n where 1le n_1,n_2,n_3le 6. Such a sum should immediately suggest a generating function, and for this example the generating function is


= x^{3} + 3x^{4} + 6x^{5} + 10x^{6} + 15x^{7} + 21x^{8} + 25x^{9} + 27x^{10} + 27x^{11}

qquad+ 25x^{12} + 21x^{13} + 15x^{14} + 10x^{15} + 6x^{16} + 3x^{17} + x^{18}.

Thus t(3,10)=27. And in general t(k,n) is the coefficient of x^n in (x+x^2+x^3+x^4+x^5+x^6)^k. This result is of course also given on Mathworld. And we can calculate these with Sage:

sage: dx = x+x^2+x^3+x^4+x^5+x^6
sage: expand(dx^3).coeff(x,10)

There’s nothing to stop us trying larger numbers of dice and larger sums, for example 10 dice and the sum of 34 for the value of t(10,34):

sage: expand(dx^10).coeff(x^34)

However, this can be slow. For large values of k Sage has to do a lot of computation. When I tried with k=100, my computer ground to a halt as all memory was sucked up by Sage, which crashed a bit later. (For those of you concerned about Sage abuse, I gave it a nice cup of tea after a restart and we’re still good friends.) So we would like a more efficient method of computing t(k,n) for large k and n.

Note that by the definition of t(k,n) as a coefficient of x^n, and as we are using 6-sided dice, if follows that

displaystyle{t(k+1,n)=sum_{i=1}^6 t(k,n-i)}.

If we let t(k,n)=0 for n less than k or greater than 6k we can produce a simple dice program:

sage: def dice(k):
    d = 6*[1]
    for i in range(k-1):
        t = 5*[0] + d + 5*[0]
        d = [sum(t[i:i+6]) for i in range(len(t)-5)]
    return d


sage: dice(2)
  [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
sage: dice(3)
  [1, 3, 6, 10, 15, 21, 25, 27, 27, 25, 21, 15, 10, 6, 3, 1]

Notice that the outcome of dice(k) gives the number of sums for all n starting at n=k. So then

sage: def t(k,n):
         return dice(k)[n-k]


sage: t(2,10)
sage: t(3,10)
sage: t(100,350)

Big enough for ya?


Thanks to a correspondent (see below) the use of polynomial rings vastly simplifies matters.

For example, without doing any programming:

sage: R.<x> = PolynomialRing(ZZ)
sage: dx = R([1]*6)*x
sage: dx
  x^6 + x^5 + x^4 + x^3 + x^2 + x
sage: (dx^3)[10]
sage: (dx^100)[350]

All results are given instantaneously. The nice thing about his approach is that it enables us to mix and match dice. Suppose we throw 11 cubical dice, 13 octahedral dice, and 15 dodecahedral dice. In how many ways can we obtain the sum of 200?

In one step:

sage: ((R([1]*6)*x)^11*(R([1]*8)*x)^13*(R([1]*12)*x)^15)[200]

31 thoughts on “Sums of dice throws

  1. nice post.
    you could use sage directly for larger k if you use a Polynomial Ring instead of the Symbolic Ring:

    def t(n, k):
    R. = ZZ[]
    p = R([1]*6)*x # x + x^2 … x^6
    return (p^k)[n]

    this still works instantly for t(5000, 1000). But you will still run into memory problems at some point.

  2. Fantabulous! I have to try reprogramming this into javascript and making a web page for it! I have done a very nice activity with Year 8 students where they throw three dice and attempt to gain some grasp over (a) the number of ways a number can be found as a sum of three dice, and (b) the most likely sum(s).

  3. Hello would you mind sharing which blog platform you’re using? I’m looking to start my own
    blog in the near future but I’m having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something unique.
    P.S Sorry for getting off-topic but I had to ask!

    1. I chose WordPress because when I started it seemed to have the best support for mathematics. And there are also WordPress blogs by some of the world’s top mathematicians (Terry Tao, for example). I don’t know what’s out there now, because I haven’t looked. WP still works for me, though.

  4. Currently it seems like Expression Engine is the preferred blogging platform
    available right now. (from what I’ve read) Is that what you are using on your blog?

  5. Its such as you read my mind! You appear to know so much approximately
    this, like you wrote the book in it or something. I feel that you just can do with some % to drive the message house a little bit, but other than that, this is great blog.
    A fantastic read. I’ll definitely be back.

  6. You’re so cool! I do not believe I have read through a single thing like this before. So nice to discover somebody with a few unique thoughts on this subject. Seriously.. thanks for starting this up. This site is one thing that’s needed on the internet,
    someone with a bit of originality!

  7. Simply wish to say your article is as astounding. The clearness for your
    post is just spectacular and that i can suppose you’re an expert on this subject. Fine with your permission allow me to clutch your RSS feed to keep updated with coming near near post. Thank you one million and please keep up the rewarding work.

  8. I have been browsing online more than 2 hours
    today, yet I never found any interesting article like
    yours. It is pretty worth enough for me. In my view, if all web owners and bloggers made good content as you
    did, the internet will be a lot more useful than ever before.

  9. Hey would you mind letting me know which web host you’re working with?
    I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot quicker then most.
    Can you suggest a good web hosting provider at a reasonable price?
    Thanks, I appreciate it!

  10. Nice post. I learn something new and challenging
    on blogs I stumbleupon on a daily basis. It will always be useful to read through content from other authors and practice a little something from their web sites.

  11. It’s the best time to make a few plans for the long ruun and it’s time to be happy.
    I’ve read this publish and if I may I wish to
    recommend you few fascinating things or suggestions. Maybe youu can write next articles relating to this article.
    I wish to leawrn more things about it!

  12. You actually make it seem so easy with your presentation buut I find this topic to be actually something which
    I think I would never understand. It seems too comolicated and extremely
    broad for me. I’m looking forward for your next post,
    I will try to get the hang oof it!

  13. Beyond hair’s-breadth, tegument and nails, the coolheaded thing that silica
    is probably to molecular screen keep dry.
    proper, so you will in spades get an alternative to experience multiple purchases.
    Some of these dry packs can even out be carcinogenic so ever continue them forth from children.

Leave a Reply

Your email address will not be published. Required fields are marked *