Probabilities with Dice

I’m contemplating programming an RPG where you roll N 6-sided dice to use a skill (the value of N comes from a statistic, and I might allow it to be as high as 10). A 5 or a 6 is a success (for a 1/3rd chance of success with 1 die). A difficult task might require multiple successes to complete.

I’ve written a program to compute just how likely all these dice rolls are to succeed. However, I’ve done this by using what little I can remember about probabilities rather than computing every possibility.

Does the following pseudocode seem like it should be correct? It’s the probability of getting exactly J successes with I dice (not more or less).

Probability(J successes with I dice) = Probability(1 success with 1 die) * Probability(J - 1 successes with I - 1 dice) + Probability(1 failure with 1 die) * Probability(J successes with I - 1 dice)

Since I know the probability of success/failure with 1 die, this is the same as:

Probability(J successes with I dice) = 1/3 * Probability(J - 1 successes with I - 1 dice) + 2/3 * Probability(J successes with I - 1 dice)

Basically, it’s a recursive formula that figures that one die is either a success or not and then factors in the probabilities of getting the right number of successes in each case.

I’ve already written the program, and I’m pretty sure it’s correct. However, I wanted to check this with someone before posting the tables on my website. I considered writing a program to use brute force to check all these numbers, but, for all I know, my brute force program might turn out to be wrong. I would rather just verify that my math is right.

I’ve pasted the tables below, though the headers aren’t formatted properly after being pasted.

Number of Successes (exact)

Dice 0 1 2 3 4 5 6 7 8 9 10
1 66.666% 33.333% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000%
2 44.444% 44.444% 11.111% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000%
3 29.629% 44.444% 22.222% 3.703% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000%
4 19.753% 39.506% 29.629% 9.876% 1.234% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000%
5 13.168% 32.921% 32.921% 16.460% 4.115% 0.411% 0.000% 0.000% 0.000% 0.000% 0.000%
6 8.779% 26.337% 32.921% 21.947% 8.230% 1.646% 0.137% 0.000% 0.000% 0.000% 0.000%
7 5.852% 20.484% 30.727% 25.605% 12.802% 3.840% 0.640% 0.045% 0.000% 0.000% 0.000%
8 3.901% 15.607% 27.312% 27.312% 17.070% 6.828% 1.707% 0.243% 0.015% 0.000% 0.000%
9 2.601% 11.705% 23.411% 27.312% 20.484% 10.242% 3.414% 0.731% 0.091% 0.005% 0.000%
10 1.734% 8.670% 19.509% 26.012% 22.760% 13.656% 5.690% 1.625% 0.304% 0.033% 0.001%

To get the probability of at least J successes, I just added the relevant probabilities together, and created another table.

Number of Successes (at least)

Dice 0 1 2 3 4 5 6 7 8 9 10
1 100.000% 33.333% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000%
2 100.000% 55.555% 11.111% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000%
3 100.000% 70.370% 25.925% 3.703% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000%
4 100.000% 80.246% 40.740% 11.111% 1.234% 0.000% 0.000% 0.000% 0.000% 0.000% 0.000%
5 100.000% 86.831% 53.909% 20.987% 4.526% 0.411% 0.000% 0.000% 0.000% 0.000% 0.000%
6 100.000% 91.220% 64.883% 31.961% 10.013% 1.783% 0.137% 0.000% 0.000% 0.000% 0.000%
7 100.000% 94.147% 73.662% 42.935% 17.329% 4.526% 0.685% 0.045% 0.000% 0.000% 0.000%
8 100.000% 96.098% 80.490% 53.177% 25.864% 8.794% 1.966% 0.259% 0.015% 0.000% 0.000%
9 100.000% 97.398% 85.693% 62.282% 34.969% 14.484% 4.242% 0.828% 0.096% 0.005% 0.000%
10 100.000% 98.265% 89.595% 70.085% 44.073% 21.312% 7.656% 1.966% 0.340% 0.035% 0.001%

If you treat them as three sided-dice where 3 is treated as 5 or 6 on the 6-sided dice, then the question becomes:
Given a random length n string over the alphabet {1,2,3}, what is the probability of j occurrences of ‘3’

Since the number of possible strings is just 3^n, all you need to know is th number of unique strings with j '3’s. This is just the number of ways of selecting j items from n items. That’s just n!/(n-j)!j!

Edit: I get the feeling that there is a flaw in my logic, but I’ve been up all night, and for the life of me I cant see it.
Ok. just realised what it was. After getting the number of ways of selecting the j ‘3’ entries, you need to multiply it by 2^(n-j), which are the possible values for the non-‘3’ entries in the string.

So the final probability is something like 2^(n-j) * ( n! / (n-j)!j! ) / 3^n

A good site for this sort of calculations can be:

Just thought I’d add that the values in the first table match the formula, so I guess they’re right.

Also, that anydice site is nice! Not sure that writing the program for it is any easier than writing the calculation in a Java program, but the output is certainly fancy =)

I must have learned this stuff 3 times, but I can’t remember any of it. I’m not sure whether your final answer is exactly right or not, but it seems like the sort of thing the answer should be.

I checked this out, and it seems to confirm my computations. It didn’t take much effort to get it to compute exactly the things I was computing. It’s a great program and easy to use.

The only bad thing I have to say about is that you can’t set the number of decimal places to include in the probabilities. For instance, the chance of getting 10 3’s when rolling 10d3 is only 0.001% (1 in 100,000), which anydice rounds down to 0. That might not be that important to most people, but going to 3 decimal places gives me probabilities that are all non-zero.

Thank you both for your help.

I’ve taken a different approach in a board-game adaption I’m currently finishing up. For 2d6, rather than generate one random number and compare against a probability table, I just generate two random numbers in the range [1,6] and add them!

Fine for a few dice in isolation, but I think the OP wants to compute compound probabilities for which the only practical solution is this approach, it’s the difference between O(1) vs O(n).

That would be fine. I’m not doing this for performance reasons. I just wanted to see what the probabilities were like. Now that I have the table, I might use it just because I have it.

Really, O(n) isn’t bad when n is never more than 10.

I think the bigger difference about my system is that each die roll is a success or failure rather than adding the dice together. I’m emulating the World of Darkness ruleset, though I’m using d6’s rather than d10’s.

Bookmark http://en.wikipedia.org/wiki/Binomial_distribution