[RPG] Using AnyDice to determine the odds of getting a specific number sequence on multiple dice

anydicestatistics

After a few hours trying to design the proper code in AnyDice, I had to admit I didn't find a proper way to reach my goal, so I'm wondering if someone could give me a helping hand.

Here is the problem :

  • I'm looking for odds of successfully rolling a specific combination with a multiple dice roll; for instance, the exact combination [1,2,5] with 3d6, or [4,3,3] with 3d6, or [1,2,2,6] with 4d6, etc. (The latter examples emphasize the fact that the same number could be present a variable number of times, as a double, triple, etc. — increased complexity.)
  • I can easily isolate the odds for some "classic" combinations (like doubles with 2d6, triples with 3d6, etc.) but what I'm searching for is a generic formula for any combination of n numbers for nd6 (or other number of faces).

Does anyone know how to code this? I'm guessing that AnyDice can do this, but I don't know how to tell it to. I would be grateful if someone could at least point me in the right direction!

Best Answer

It is, in fact, possible to do this with AnyDice:

function: compare A:s and B:s {
  result: A = B
}
output [compare 4d6 and {5, 2, 2, 1}]

The trick here is that, when passing a die (like 4d6 here) to a function expecting a sequence, AnyDice will call the function for every possible (sorted) roll of the dice, and return a distribution (i.e. a biased die) giving the probability of obtaining each result.

(Note that the literal sequence {5, 2, 2, 1} needs to be sorted from highest to lowest value, since that's AnyDice's default sorting order for dice rolls.)


Running the above program gives about 0.93% for the probability to roll [1, 2, 2, 5], in any order, on 4d6. To double-check the result, we can calculate the probability by hand, and compare it to the AnyDice result:

The first thing to realize is that, as long as our dice are fair, the actual numbers in the sequence are irrelevant: we would get the exact same probability for rolling [1, 2, 2, 3] or [2, 5, 5, 6], or even [4, 5, 6, 6]. All that matter is the fact that there are four dice, each with six sides, and that we're interested in the probability of rolling a specific combination, where two of the rolls are equal, in any order. Below, I will keep using [1, 2, 2, 5] as an example, but it's important to keep in mind that we would get the same result for any such set of rolls.

The rest is simple combinatorics:

  • There are 64 = 1296 possible (and equally likely) results of rolling 4 six-sided dice in sequence. We want to know how many of those sequences, when sorted, yield [1, 2, 2, 5].

  • There are 4! = 4 × 3 × 2 × 1 = 24 ways to shuffle a set of four distinct numbers: we can put the smallest number in any of the four positions, then the next-smallest in any of the three remaining ones, and so on. Thus, if all the numbers in the combination we wanted were distinct, there would be 4! = 24 ways to roll it, and thus the probability of rolling it would be 4! / 64 = 24 / 1296 = 1/54 ≈ 1.85%.

  • However, in the set [1, 2, 2, 5], two of the numbers are equal. What to do? Well, let's pretend for a moment that the numbers are actually, say, [1, 2a, 2b, 5], where the labels "a" and "b" have been added to make the two "2"s distinct.

    Now we have four distinct items, which we can shuffle in 4! = 24 different ways. If we then drop the extra labels we added to the numbers to make them distinct, we get an arrangement of the original numbers: for example, [5, 2a, 1, 2b], with the labels removed, gives us [5, 2, 1, 2]. But so does [5, 2b, 1, 2a]! In fact, for every arrangement of [1, 2, 2, 5], there are two arrangements of [1, 2a, 2b, 5] that yield it when the labels are removed: one that has 2a before 2b, and one that has them the other way around.

    Thus, if two of the numbers in our set are equal, then there are only 24 / 2 = 12 ways to shuffle it, and thus only 12 of the 1296 possible sequences of 4d6 will yield it when sorted. Thus, the probability of rolling [1, 2, 2, 5] on 4d6, in any order, is 12 / 1296 = 1/108 ≈ 0.93%, just as AnyDice told us.

We can apply the same reasoning for sets with more duplication. For example, in the set [1, 1, 5, 5], we have two pairs of equal numbers. We can use the same trick of labeling them as [1a, 1b, 5a, 5b] before shuffling them, but now each unlabeled sequence corresponds to 2 × 2 = 4 labeled sequences. Thus, the number of ways to roll [1, 1, 5, 5] on 4d6, in any order, is 24 / 4 = 6, and thus the probability or rolling it is 6 / 1296 = 1/216 ≈ 0.46%.

What about the case of three equal numbers, as in [1, 2, 2, 2]? Again, we can relabel the numbers as [1, 2a, 2b, 2c], and have 24 ways of shuffling them, but now there are more than two ways to arrange the "2"s. How many more? Well, the answer is given by the same formula we used to count the total number of arrangements above: the number of ways to sort three distinct items (here, [2a, 2b, 2c]) is 3! = 3 × 2 × 1 = 6. Thus, there are only 24 / 6 = 4 ways to arrange the unlabeled set [1, 2, 2, 2] — a fact which is easily verifiable by hand — and thus the probability of rolling it is 4 / 1296 = 1/324 ≈ 0.31%.

Finally, of course, for four equal numbers, as in [1, 1, 1, 1], there's clearly just one way to roll them, and the probability is thus 1/1296 ≈ 0.077%.

Here's the same information, conveniently tabulated:

Probability of rolling a specific set of numbers on 4d6, in any order:

All numbers distinct: 24 / 1296 = 1/54  ≈ 1.85%
Two numbers equal:    12 / 1296 = 1/108 ≈ 0.93%
Two equal pairs:       6 / 1296 = 1/216 ≈ 0.46%
Three numbers equal:   4 / 1296 = 1/324 ≈ 0.31%
Four numbers equal:    1 / 1296         ≈ 0.077%

You can use the same method to work out similar probabilities for any number of dice of any kind. For example, the number of distinct ways to shuffle the set [2, 2, 3, 3, 3] is 5! / (2! × 3!) = 120 / (2 × 6) = 10, while the total number of ways to roll 5d6 in sequence is 65 = 7776, giving a probability of 10 / 7776 ≈ 0.13% of rolling that combination, in any order, on 5d6.

(And yes, this method is equivalent to the multinomial coefficients suggested by limsup; by definition, Multinomial(n1, n2, ..., nk) = (n1 + n2 + ... + nk)! / (n1! × n2! × ... × nk!).)


Ps. In the answer above, I assumed that you were interested in the probability of rolling a specific set of number in any order, so that e.g. [1, 2, 5, 2] and [1, 2, 2, 5] would be considered equivalent outcomes, but [2, 3, 3, 6] would be considered a different outcome.

If you instead want the combined probability of rolling any combination that contains exactly one pair of equal numbers, whatever those numbers are, you'll need to multiply the probability of rolling any one of those combinations (calculated above) with the total number of such combinations.

How many such combinations are there? There are several ways to calculate that, but a fairly simple one is to:

  • First count the number of ways to choose three distinct numbers (in any order) out of the six on the die: that's such a common thing to do in combinatorics that there's a special notation for it: 6C3, read as "6 choose 3". (There's a more common notation that's written sort of like "(63)", but with the 6 directly above the 3; alas, I can't type it properly here.) This number can most easily be calculated as the number of ways to choose three distinct items out of six in order, i.e. 6 × 5 × 4 = 6! / (6−3)!, divided by the number of ways to shuffle the chosen items, i.e. 3 × 2 × 1 = 3!, which yields 6C3 = (6! / (6−3)!) / 3! = 120 / 6 = 20.

  • Next, multiply this number by three, to account for the fact that we can choose any one of the three numbers to be doubled. Thus, the total number of combinations of four numbers between 1 and 6, with exactly one pair of the numbers equal, is 6C3 × 3 = 60, and thus the probability of rolling such a combination on 4d6 is 60 × 12 / 1296 = 60 / 108 ≈ 55.6%.

Similarly, the number of combinations of four distinct numbers between 1 and 6 can be calculated as 6C4 = (6! / (6−4)!) / 4! = (6 × 5 × 4 × 3) / (4 × 3 × 2 × 1) = 360 / 24 = 15, giving a total probability of 15 × 24 / 1296 ≈ 27.8% of rolling four different numbers with 4d6. (Note that this is less than the probability of rolling a single pair, even though each of the four-different combinations is individually more probable than the one-pair combinations, simply because there are fewer combinations where all the dice are different than ones where two are the same.)

For two pairs, we get 6C2 = 15 possible combinations (since we need to choose two numbers out of six, and they're both doubled so we don't need to make any additional choices about that), for a total probability of 15 × 6 / 1296 ≈ 6.94%. (It's not a coincidence that 6C2 = 6C4; a moment of thought should make it obvious that choosing two items out of six is equivalent to choosing four and leaving two behind.) For three of a kind, the number of combinations is 6C2 × 2 = 30 (we choose two distinct numbers, and triple one of them), and so the total probability is 30 × 4 / 1296 ≈ 9.26%, while for four of a kind, we have just 6C1 = 6 choices, for a total probability of 0.46%. Or,in tabular format:

Probability of rolling any result with a given pattern of equal/distinct numbers with 4d6, in any order:

All numbers distinct: 15 × 24 / 1296 ≈ 27.78%
Two numbers equal:    60 × 12 / 1296 ≈ 55.56%
Two equal pairs:      15 ×  6 / 1296 ≈  6.94%
Three numbers equal:  30 ×  4 / 1296 ≈  9.26%
Four numbers equal:    6 ×  1 / 1296 ≈  0.46%

Pps. Yes, we can do this in AnyDice, too, but it's a bit more complex. Here's an example for various outcomes of rolling 4d6:

function: unique values in S:s {
  U: {}
  loop N over S { if !(N = U) { U: {U, N} } }
  result: U
}  
function: find PATTERN:s in ROLL:s {
  COUNTS: {}
  loop N over [unique values in ROLL] {
    COUNTS: {COUNTS, N = ROLL}
  }
  result: [sort PATTERN] = [sort COUNTS]
}
output [find {4} in 4d6] named "four of a kind"
output [find {3, 1} in 4d6] named "one triple, one single"
output [find {2, 2} in 4d6] named "two pairs"
output [find {2, 1, 1} in 4d6] named "one pair, two singles"
output [find {1, 1, 1, 1} in 4d6] named "all singles"

Look at the graph labeled "1" in the Transposed view to see all the probabilities nicely in a single bar graph.

Ppps. Here's the same thing for 6d6.

Related Topic