# Using AnyDice to determine the odds of getting a specific number sequence on multiple dice

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!

• Are you looking for them in order, or just the results being that set? – wax eagle Jan 26 '15 at 19:07
• Also, it's probably worth asking, is there a reason you need these in Any dice and aren't just working out the probabilities by hand (which actually might be easier in this particular case) – wax eagle Jan 26 '15 at 19:22
• Usually such questions (aside the context of specific software tools like AnyDice) are typically also on-topic at either math.SE or stats.SE, so one of those would be an alternative place to ask for this type of question. – Glen_b Jan 27 '15 at 5:35
• Do you need help for sequences with other die types? I.e. if you are rolling 6d8 or maybe even 2d6+3d8? – limsup Jan 27 '15 at 21:30

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.

# Probability for a special combination on nd6:

1. If the sequences (1,2,3) and (1,3,2) are not the same - distingishable dice - then each sequence has the same probability of (1/6)^n where n gives the number of dice thrown. So

$$P(1,2,3)=P(3,2,1)=P(2,3,1)=\frac{1}{216}.$$

Or general

$$P=\left(\frac{1}{6}\right)^n.$$

2. If the sequences (1,2,2,3) and (2,1,2,3) are the same - undistingishable dice - then you need to count each sequence to get the probability. Mathematically you can use the multinomial coefficient to count those different combinations. So

$$P(1,2,2,3) = \mathrm{Multinomial}[1,2,1] \cdot \frac{1}{216} = \frac{12}{216}=\frac{1}{18}.$$

Or general

$$P=\mathrm{Multinomial}[\text{number of 1s, number of 2s, number of 3s,..., number of 6s}] \cdot \left(\frac{1}{6}\right)^\text{(number of dice)}.$$

3. If it is not important what numbers are in the sequence and (1,2,2,3) is the same as (2,4,4,6) and (2,2,4,4) is the same as (1,1,5,5) you can count the possible faces of the dice in the sequence with $6 \cdot 5 \cdot \dots$ etc. That gives you

$$P(double, double)=\mathrm{Multinomial}[2,2] \cdot 6 \cdot 5 \cdot \left(\frac{1}{6}\right)^4$$

and

$$P(single, single, triple)=\mathrm{Multinomial}[1,1,3] \cdot 6 \cdot 5 \cdot 4 \cdot \left(\frac{1}{6}\right)^5.$$

Or general

$$P=\mathrm{Multinomial}[\text{length of 1st set, length of 2nd set,...}] \cdot \frac{6!}{(6-\text{number of sets})!} \cdot \left(\frac{1}{6}\right)^\text{(number of dice)}.$$

• Your #1 makes sense, but your #2 makes less sense. How do you go from P(1,2,2,3) to MultinomialCoefficient(4 choose 1,2,1)*1/216? How would you phrase that generically so that it could be used for other combinations of numbers? Also, if I plug "MultinomialCoefficient(4 choose 1,2,1)*1/216" into Wolfram Alpha, it gives me 35/72, not 1/12 as you say here. Is it more complicated than just copying the formula into the website? – DuckTapeAl Jan 26 '15 at 21:00
• w|a code for multinomial coefficient is Multinomial[1,2,1]. If you have the sequence a,b,b,c you have 1xa and 2xb and 1xc wherefor Multinomial[1,2,1]. – limsup Jan 26 '15 at 21:31
• Okay, but three things; First, Multinomial[1,2,1]*1/216 = 1/18, not 1/12, according to w|a. Second, how do you generalize that to other dice combinations? For example, what would the correct formula be for (1,2,3,4)? Or (1,3,3,3)? You say to use the multinomial coefficient, but you don't really say how to use it. Third, if you're suggesting to use w|a for these calculations, you should give the correct w|a format in your answer. – DuckTapeAl Jan 26 '15 at 21:38
• Sorry for the typ error with 1/12 and 1/18, changed! I also added general formulas. – limsup Jan 26 '15 at 22:08
• @DuckTapeAl To count the number of ways to roll 1,2,2,3 (for example), you need to count the number of ways to choose two of the four dice to be the 2's, then choose one of the last two dice to be 3, and then the last die must be the 1. This process is exactly what the Multinomial function calculates – starchild Jan 27 '15 at 1:59