Probability of being able to make a given number with two out of xd6

I'm an amateur games designer, and am working on a mechanic which involves rolling on a table with numbers running from 2-12 - the full range of possibilities from adding together two six-sided dice. Calculating the probability of any given result on 2d6 (or any other number of dice, when all of them are summed) is obviously pretty simple, but I'm looking for a general way to calculate a slightly weirder case: the probability of being able to make a given number using exactly two of the dice rolled in a larger pool.

Rather than just rolling 2d6, you roll a number of dice depending on a few other game factors (between three and five) and choose two of them, add the rolls together, then consult the relevant result on the table. So if I were to roll three dice and get 2, 3 and 5, I could choose the entry for 5, 7, or 8, depending on which two of the three I decide to add together.

The issue I'm having is determining the probability that I can make a particular number, which obviously depends on working out how many permutations there are from which that number can be found. For instance, there are sixteen possible rolls that allow one to make 2 with two dice from 3d6 - five 1,1,!1 combinations, five !1,1,1s, five 1,!1,1s, and 1,1,1.

I was able to work this out for 3d6 "the hard way" just by writing out a great big table of all the possibilities and ticking off the ones that worked - so I found, for example, that the probability of being able to make 7 is $\frac{90}{216}$, or $\frac{5}{12}$, or roughly 41.7%. But for four and five dice (five being the maximum I'm going to need), checking through 1296 or 7776 possibilities doesn't exactly appeal. Is there a general formula or some other process I can use to calculate, say, my chances of being able to make 7 with exactly two of the four dice I've rolled?

This can be done using inclusion-exclusion. I don't see how to write it in general form, but since you say that you won't need more than five dice, I'll work it out for you for three and four dice and then perhaps you can do it yourself for five.

We need to treat odd and even numbers separately, because the possibility of adding two equal numbers makes a difference. Let's start with the odd numbers, which are easier.

For three dice, we have three different pairs of dice. Say we want the probability of an odd number that can be written as an unordered sum in $k$ different ways (so for $3=1+2$ and $11=5+6$ this would be $k=1$, for $5=1+4=2+3$ and $9=4+5=3+6$ it would be $k=2$ and for $7=1+6=2+5=3+4$ it would be $k=3$).

One pair: There are $\binom32=3$ pairs, each has $2k$ allowed values, and the remaining die has $6$ values, so the first contribution is $3\cdot2k\cdot6=36k$.

Two pairs: There are $\binom32=3$ pairs of pairs, and each has $2k$ allowed values, so the second contribution is $3\cdot2k=6k$.

Three pairs: There are no values that make all three pairs add up to the same odd number.

Thus, the total is $36k-6k=30k$, in agreement with your result of $90$ for $k=3$.

So let's do the same thing for four dice.

One pair: There are $\binom42=6$ pairs, each has $2k$ allowed values, and the remaining dice have $6^2=36$ allowed values, for a contribution of $6\cdot2k\cdot36=432k$.

Two pairs: There are two possibilities. If the pairs are disjoint, there are $3$ ways to choose them, and each has $2k$ options, for a contribution of $3\cdot(2k)^2=12k^2$. If they overlap, there are $\binom62-3=12$ ways to choose them, they have $2k$ options, and the remaining die has $6$ options, for a contribution of $12\cdot2k\cdot6=144k$.

Three pairs: They can either all have one die in common; there are $4$ choices for that die, and $2k$ options in each case, for a contribution of $4\cdot2k=8k$. Or they can form a chain linking all four dice. There are $12$ such chains, and in each case $2k$ options, for a contribution of $12\cdot2k=24k$.

For four pairs, the chains become cycles, and there are $3$ such cycles (each corresponding to $4$ different chains depending on where we break it), again with $2k$ options in each case, for a contribution of $3\cdot2k=6k$.

Thus the total is $432k-12k^2-144k+8k+24k-6k=314k-12k^2$, so for instance the chance to be able to form a $7$ is $(314\cdot3-12\cdot3^2)/6^4=834/1296=139/216$.

Now the even numbers. We already know the chance of being able to form them by adding different numbers, so we just need to add the chance of being able to form them by adding equal numbers while not being able to form them by adding different numbers.

For three dice, this is easy, since if we can form an even number using equal numbers, we can't be able to form it by adding different numbers, so we just need the number of rolls that allow us to form it with equal numbers. This is $3\cdot6-2=16$, since there are $3$ pairs to choose from, the remaining die has $6$ options, and then we have to subtract $2$ because we counted the roll with all three dice equal three times. Thus, for an even number that can be written as a sum of different numbers in $k$ ways, there are $30k+16$ out of $6^3$ rolls from which it can be formed.

For four dice, it's slightly more complicated. If we have two copies of the required number, there are $\binom42=6$ ways to choose those dice, and then $5^2-2k=25-2k$ options for the other two dice, for a contribution of $6\cdot(25-2k)=150-12k$. If we have three copies, there are $\binom43=4$ ways to choose them, and the remaining die has $5$ options, for a contribution of $20$. And then there's one case in which we have four copies, for a total of $150-12k+20+1=171-12k$. Thus, together with the result for odd numbers, that makes $314k-12k^2+171-12k=171+302k-12k^2$ rolls from which an even number that can be written as the sum of different numbers in $k$ ways can be formed.

Here's a summary of the results:

\begin{array}{c|cc} &\text{three dice}&\text{four dice}\\\hline 2&16&171\\ 3&30&302\\ 4&46&461\\ 5&60&580\\ 6&76&727\\ 7&90&834\\ \end{array}

Here's Java code that checks these results by enumerating all rolls. Here are the results of the enumeration for five dice (spoiler-proof in case you want to calculate them by hand first; hover over the text area to display them):

2: 1526
3: 2550
4: 3816
5: 4620
6: 5626
7: 6210