# Probability of having $n$ dice equal to or greater than $x$ when $m$ dice are rolled

I am trying to figure out how to do the math for something like this. The scenario:

• $n = 4$
• $x = 7$ (using d10s, where 0 = 10)
• $m = 8$

In words, if I roll 8d10 (eight 10-sided dice) what is the probability of having four dice greater than or equal to 7 (where 0 is the greateest [10])

I have seen a lot of sites which will do the calculation where $n = 1$, but I want to make $n$ a variable.

I know the probability of a 'successful' role is 4/10.

I know that I could write out all the possible combinations and count the ones that meet the criteria, but I'm sure this can be done with math.

How would you calculate this?

## migrated from stackoverflow.comNov 26 '16 at 5:07

This question came from our site for professional and enthusiast programmers.

Probability (in this case) is the ratio of desired outcomes to all outcomes. Let's count desired outcomes in your particular case (and by the way we derive a general formula to compute it).

Suppose you roll 4 dice and you have a total success - on every die you get "7 to 10". How many quadruplets of numbers "1 to 10" may cause it? It is not so difficult: $$4 \cdot 4 \cdot 4 \cdot 4$$ or $$4 ^ 4$$.

Then you roll the 4 remaining dice - but now you need a total failure - numbers 1 to 6 on every die. How many quadruplets is able to cause it? The answer is similar: $$6 ^ 4$$.

So for the first group of four dice to be totally successful and in the same time the second group of four dice to be totally unsuccessful - e. g. $$(8, 8, 8, 8, 1, 1, 1, 1)$$ - there is $$4^4 \cdot 6^4$$ possibilities.

But where is written that the first four dice have be successful (and in the same time the four others not)? We may select four successful dices by $$_8 \mathop{C}_4 = 70$$ ways. So all successful possibilities count $$70 \cdot 4^4 \cdot 6^4$$ which gives the result $$23{.}224{.}320$$.

So the general formula for the number of desired outcomes is
$$_n {\mathop{C}}_k \cdot p^k \cdot q^{n-k}$$

Note: Please don't forget to divide it by the number of all possibilities, i. e. by $$(p + q)^n$$ to compute the probability.

• Thank you, that does a great job of explaining how the equation works, and it's parts. I tried to apply it to the problem of having N OR MORE die which meet the criteria of being greater than X, but using the same format, I don't get the result I expect. Perhaps I have done something wrong or it requires different logic. – pcaston2 Nov 10 '16 at 18:22
• @pcaston2: No, there is no nice exact formula for partial binomial sums. For approximate formulas see the incomplete beta function and numerical approximations for that. – LutzL Nov 10 '16 at 19:01
• @LutzL Thanks. I wrote two algorithms for calculating this, one by exhausting all possibilities and testing if they are successful, and the other is empirical by rolling the "die" and finding the success count. I got 40% for going through all situations (which seems high) and 25% or so after millions of iterations. I'm surprised by the difference in these two. – pcaston2 Nov 10 '16 at 19:09
• Adding the formula values for the cases gives 40.59136% as probability. The 25% result is mysterious, perhaps with code one could find the error, a million iterations should give about 3 valid digits. – LutzL Nov 10 '16 at 19:57

It is so called Binomial distribution ($$2$$ outcomes: "$$7$$ to $$10$$" and "$$1$$ to $$6$$" with probabilities $$0.4$$ and $$0.6$$, respectively), and what you want is a binomial experiment, for which the formula is $$_{n}C_{k} \cdot p^k \cdot (1 - p)^{n - k}$$

where

• $$n$$ is the number of trials ($$8$$)
• $$k$$ is the number of required successful trials ($$4$$)
• $$_{n}C_{k}$$ is number of combinations of $$k$$ elements from $$n$$ elements ($$_8C_4 = 70$$)
• $$p$$ is the probability of the success in one trial ($$0.4$$)

So you end up with the value (approximately) $$0.2322432$$

(I calculated it in Python with the following commands:

import itertools

p = .4
n = 8
k = 4

nCk = len(list(itertools.combinations(range(n), k)))

result = nCk * pow(p, k) * pow(1 - p, n - k)

print(result)

• That looks much easier than writing everything out. Before I saw your comment, I decided to write an app to calculate these by going through each state It is a simple algorithm. I check to see if a state is 'successful' by counting the die > X and if they match or exceed M it works. Then I add 1 to the left-most state. If it overflows, it resets to state 1 and increments the next state. If the last state resets, it's the end. I wind up with 40,591,360 successful states and 100,000,000 which is about 40.59%. Maybe my algorithm is wrong, or my logic, or something? – pcaston2 Nov 10 '16 at 15:16
• I don't mean to be nit-picky, but the first equation "nCk * p ^ n...." should be "nCk * p ^ k...." ? This appears to be what you put in your python code, and seems to yield more accurate results. – pcaston2 Nov 10 '16 at 15:42
• This may be a result of me muddling something up, but for (p,n,k) combination (0.4,8,3) I get 27.8% but for (0.4,8,2) I get 20.9%. The probability shouldn't decrease when I lower the number of successful trials. – pcaston2 Nov 10 '16 at 16:01
• @pcaston2: It is very important to describe the probabilisic event carefully. Do you want exactly 4 dice with the large values, which has probabilities given in the answer or do you want at least 4 dice with the large values, where you need to add the probabilities for exactly 4,5,6,7 and 8 dice with large values. – LutzL Nov 10 '16 at 17:54
• See stackoverflow.com/questions/3025162/… for faster ways to compute nCk than producing the list of all combinations and counting them. – LutzL Nov 10 '16 at 18:00