Probability of s-sided die with n-rolls summing to p excluding die that rolled a 1

I found this really useful formula from the dice article on mathworld.

Here is my version in Python.

def P(p, n, s):
"""
Taken from: http://mathworld.wolfram.com/Dice.html
:param p: sum of rolls
:param n: num rolls
:param s: num sides
:return: The probability of summing to p with n rolls of an s-sided die
>>> P(7, 2, 6)
0.16666666666666666
"""
return 1 / s ** n * sum([(-1) ** k * ncr(n, k) * ncr(p - s * k - 1, n - 1) for k in range(0, int((p - n) / s) + 1)])


The domain of my problem will be restricted to this

• s=4: 1 <= p <= 40, 1 <= n <= 10
• s=6: 1 <= p <= 60, 1 <= n <= 10

However, in the game I'm using this algorithm for, there is a special rule for if I roll a 1. The rule is that you score a 0 if there is a 1 in any of the rolls. For example, if you roll 1, 5, 6, your sum is 0, not 12.

How do I exclude any roll containing a 1? I'm very new to probability and statistics, so I thought I could just multiply the answer by the probability of not rolling a 1, but I don't know if this is correct.

For example, if my probability is H(p,n,s), it should be:

H(p, n, s) = P(p, n, s) * ((s - 1)/ s) ** n


Where (5 / 6) ** n is the probability of not rolling a 1 n times in a row.

TL;DR: How do I get the probability of summing to p with n rolls an s-sided die excluding any sum that includes a roll of 1.

• Why on earth did you use different variables in the title than in the body? (The ones in the body correspond to the MathWorld article, so the ones in the title should be fixed.) – joriki Sep 8 '15 at 19:29
• I don't understand why the sum for 4, 5, 6 is $0$ -- in the preceding sentence, you say that the score is $0$ if there's a $1$ in any of the rolls -- but there isn't? – joriki Sep 8 '15 at 19:31
• @joriki accident. Thank you. – michaelsnowden Sep 8 '15 at 19:32
• @joriki wow I really messed up. Thanks for catching that – michaelsnowden Sep 8 '15 at 19:33

Your fix doesn't work directly as suggested, but it's not too far from the right fix. You can calculate the probability of rolling the right sum with rolls other than $1$: $P(p-n,n,s-1)$. Then you can apply your fix to correct for the fact that the actual number of possible outcomes is $s^n$, not $(s-1)^n$:
$$P(p-n,n,s-1)\left(\frac{s-1}s\right)^n\;.$$
• @doctordoder: To see why it needs to be done this way and not your way, consider e.g. the case $p=6$, $s=6$, $n=1$, where your approach yields $5/36$ whereas the right answer is $1/6$. – joriki Sep 8 '15 at 19:54