I'm building an app (for those curious, for DnD) and I came across an issue with some math I did. I need to know the probability of rolling a certain number when there are two or more different sided die used.

A concrete example that I'm working on is rolling at least 3 using a 4-sided die and a 6-sided die. Below is a table I made that contains the answer (I used Google Sheets and counted cells), but I actually need to know how to get to this answer in code. I just can't seem to find a way to work forward or backwards and get the right results. Can anyone help me figure this out?

>= 2 | 24   100
>= 3 | 23   95.83
>= 4 | 21   87.5
>= 5 | 18   75
>= 6 | 14   58.33
>= 7 | 10   41.67
>= 8 | 6    25
>= 9 | 3    12.5
>=10 | 1    4.17
  • $\begingroup$ There are ways to generate expressions for these probabilities mathematically, but in terms of actually coding them in, I think you'd be better off just enumerating the possibilities and adding them up on the fly in your code. $\endgroup$ Aug 2 '15 at 5:11

tldr the final formula for a dA and a dB with a target sum of at least N will be: $$\begin{cases} \frac{AB-T(N-2)}{AB}&\text{if}~ N-1\leq A~\text{and}~N-1\leq B\\ \frac{AB-T(N-2)+T(N-A-2)}{AB}&\text{if}~N-1>A~\text{and}~N-1\leq B\\ \frac{AB-T(N-2)+T(N-B-2)}{AB}&\text{if}~N-1\leq A~\text{and}~N-1>B\\ \frac{AB-T(N-2)+T(N-A-2)+T(N-B-2)}{AB}&\text{if}~N-1>A~\text{and}~N-1>B \end{cases}$$

If approaching by brute force, sometimes it is a whole lot easier to find out the probability of not satisfying your property. Take your example of "at least 3". The only way that you don't get at least three is by rolling a one on both dice, which occurs with probability $\frac{1}{4\cdot 6}$. Similarly, not getting at least a four would occur either with a (1,1), a (1,2), or a (2,1) for a probability of $\frac{3}{24}$.

Lets see if we can generalize. Suppose there were two six sided dice and we were looking for the probability of getting "at least a six."

enter image description here

You can see that the black numbers are below six and should be thrown out, yet all numbers above the green line are good (including those not displayed in the picture). There are a total of $6\cdot 6=36$ possibilities which are equally likely. You can notice that the black numbers form a triangle with each layer having one more possibility. We can count how many spaces are used in a triangle quickly as these are what are known as triangle numbers. In this case, there are four layers to our triangle so there are $T(4)=\binom{5}{2}=10$ spaces used. Thus in this example our probability is $\frac{10}{36}$ to not get at least a six, so it is $\frac{36-10}{36}=\frac{26}{36}$ to get at least a six.

So, this works well and good if our line happens to lie within the realm of possibility for both dice, but what happens if we change the number or the size of the die?

enter image description here

In this example, we consider the probability of rolling at least an 8 on a d4 and a d9 (yes, I know that d9 don't normally exist... work with me). You can see two triangles here. The triangle made up of all of both of the black and red numbers, as well as a triangle made of only red numbers. We count the number of spaces used in the larger triangle as $T(6)=\binom{7}{2}=21$ and we wish to throw these away. We cannot throw all of them away however since the red numbers weren't possible in the first place. We count $T(2)=\binom{3}{2}=3$ of these, so there are $18$ we wish to not consider. That is to say, the probability of not rolling at least an 8 is $\frac{(21-3)}{9\cdot 4}$, so the probability of rolling at least an 8 is $\frac{9\cdot 4 - (21-3)}{9\cdot 4}$

Similarly we might have to remove two red triangles if our target number is too high for both dice like in the following scenario:

enter image description here

In this example we consider the question "what is the probability of rolling at least a 7 on two d4." We approach again via the opposite question of what the probability is for rolling a 6 or less using the black triangle. The black triangle is of size $T(5)=15$ but there are two red triangles each of size $T(1)$ to remove for a total probability of $\frac{13}{16}$ that you don't roll at least a 7, meaning there is a probability of $\frac{3}{16}$ that you do roll at least a 7.

We can generalize all of this in the following formula.

With two dice, one with $A$ number of sides, and the other with $B$ number of sides, the probability of rolling at least a sum of $N$ is:

$$\begin{cases} \frac{AB-T(N-2)}{AB}&\text{if}~ N-1\leq A~\text{and}~N-1\leq B\\ \frac{AB-T(N-2)+T(N-A-2)}{AB}&\text{if}~N-1>A~\text{and}~N-1\leq B\\ \frac{AB-T(N-2)+T(N-B-2)}{AB}&\text{if}~N-1\leq A~\text{and}~N-1>B\\ \frac{AB-T(N-2)+T(N-A-2)+T(N-B-2)}{AB}&\text{if}~N-1>A~\text{and}~N-1>B \end{cases}$$

Where again, the formula for the triangle number is given as $T(k) = \binom{k+1}{2} = \frac{k^2+k}{2}$

The same method can work if generalizing to three dice using tetrahedral numbers instead, chopping off each corner of our three dimensional grid as necessary.

  • $\begingroup$ Thank you SOOO much. I haven't done anywhere near this level of math in ages; the diagrams and formulas really helped out. I have my code set up for 2 die now! I'm going to experiment with add three and four, but I think I can figure everything out based on what you've given me. $\endgroup$
    – gatlingxyz
    Aug 2 '15 at 6:42
  • $\begingroup$ For anyone that comes through and would like to see what I coded from this, here's a link to it: javastub.com/646530217 $\endgroup$
    – gatlingxyz
    Aug 2 '15 at 16:05
  • $\begingroup$ I realized that there may be some error catching that needs to occur in the case that N is larger than A+B, i.e when it is an impossible scenario. I'm afraid the formula I gave will spit out negative numbers. I'm on the road from church headed to family dinner, so I can't confirm, but be aware that may be an issue. $\endgroup$
    – JMoravitz
    Aug 2 '15 at 16:21
  • $\begingroup$ That's perfectly fine; if N > A+B, it's impossible as you stated so I'll catch it before it gets to the formula. $\endgroup$
    – gatlingxyz
    Aug 2 '15 at 19:05

expand $(x+x^2+x^3+x^4)(x+x^2+x^3+x^4+x^5+x^6)$

The coefficients of $x^k$ will give you the # of ways to get a sum of exactly k

[ How to get the $\ge$ form you need should be obvious ]


You could get a sum of 5, say, as 1+4, 2+3, 3+2 and 4+1

You can see that this corresponds exactly to adding up the coefficients of $x^5$

$x\cdot x^4 + x^2\cdot x^3 + x^3\cdot x^2 + x^4\cdot x$ in the expression

  • $\begingroup$ Given the OP's question, explaining why this method works and how to get an intuition derivation for it may be really useful. $\endgroup$
    – Clement C.
    Aug 2 '15 at 5:09
  • 1
    $\begingroup$ Thanks, added a PS as per your suggestion. $\endgroup$ Aug 2 '15 at 5:30
  • $\begingroup$ This was the first answer that made sense, I just couldn't figure out how to turn this into code. Thank you, though! It's was still very very helpful in understanding how it all works. $\endgroup$
    – gatlingxyz
    Aug 2 '15 at 6:42

I'm not sure what you are asking, but for your table of values, for a minimum total $n$ the odds are $f(n)=(n-1)(n-2)/48$ for $n<=6$, and $(12-n)(11-n)/48=f(13-n)/48$ for $n>6$.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.