How to calculate the probability of rolling a (six-sided) dice and having b dice be x or lower

diceprobability

I'm trying to find a formula that would allow me to calculate the odds of having B (six-sided) dice out of A rolls be X or less, where X can be a different value for each die that's rolled. If anyone is interested in why, there's a game called Axis and Allies that has combat in the form of rolling 1 die per unit, with different units dealing damage for different rolls. For example, infantry do damage on a 1 or 2, while bombers do damage on a 1, 2, 3, or 4. I'm trying to create a program that will give me the odds of winning for variable numbers and types of units, and calculating the odds of X out of B troops do damage is the area I'm stuck on.

For an example, let's say I'm going to roll 5 dice and want exactly 3 of them to be X or less. X will vary for each die, for dice 1&2 X=2, for die 3 X=3, and for dice 4&5 X=4.

I know I can use Pascal's triangle to find out how many possibilities there are for having 3 of my 5 dice roll X or less. (For example, Yes,No,Yes,Yes,No.) This means that there's 10 possible ways to have 3 dice be X or less. I also know that I can manually calculate the probability by multiplying the likelihood of each die succeeding or failing for each combination and then adding the probability of each combination.

What I'm looking for is a formula or program that will allow me to change all these variables so I can quickly calculate larger scenarios.

Thanks in advance for your help!

Best Answer

The probability of rolling any given side of a six sided die is equal, so the probability of rolling any of certain collection of numbers, e.g. $\{1,2,3,4\}$ is just the size of the collection over 6, here $4/6 = 2/3$. In your case it seems like you want to feed in a finite sequence of $N$ numbers between 1 and 6 and want to know the probability that if you roll $N$ die that the $j$the roll is $\leq$ the $j$th element of your sequence precisely $b$ times. Since the probability of rolling any given sequence is equal, really we just want to count how many ways this can happen and they will divide by the total number of possible dice roll outcome, which is just $6^N$.

Some notation: call the sequence of upper bounds $X$ and the sequence of dice rolls $D$, so these are both sequences of length $N$ and we are looking to count the number of $D \in \{1,...,6\}^N$ s.t. $D_j \leq X_j$ precisely $b$ times.

This is perhaps hard to immediately count, but it we fix any size $b$ subset $S$ of $\{1,...,N\}$, we can count the number of ways that $D_j \leq X_j \iff j \in S$. Then we just sum over all these $S$.

For some fixed such $S$, we see that $D_j \leq X_j \iff j \in S$ iff for $j \in S$, $D_j \in \{1,...,X_j\}$ and for $j \not \in S$, $D_j \in \{X_j+1,...,6\}$. Thus there are $\left(\prod_{j\in S} X_j\right) \left(\prod_{j \in S^C} (6-X_j)\right)$ different $D$ such that $D_j \leq X_j \iff j \in S$.

Therefore the total number of ways to satisfy your property is $\sum_{S \subseteq \{1,...,N\}, |S| = b} \left(\prod_{j\in S} X_j\right) \left(\prod_{j \in S^C} (6-X_j)\right).$ The probability is then that quantity over $6^N$. This can be easily computed with code.

Edit: Here's Python code.

import itertools
#number of die rolled
N=5
#your number b
b=2
#your vector of upper bounds
X=[1,1,1,1,1]
number_of_ways = 0
#make a list of integers 0,...,N-1
base_set =[]
for i in range(N):
    base_set.append(i)
#some error checking
if len(X) != N:
    print("Make your X vector the right length")
if not (0 <= b <= N):
    print("b not in correct range")
#iterate throough the subsets of {0,...,N-1} of size b and add up the number of ways as I describe
for S in list(itertools.combinations(base_set,b)):
    temp = 1
    for i in range(N):
        if i in S:
            temp = temp*X[i]
        else:
            temp = temp*(6-X[i])
    number_of_ways+= temp
#normalized by the total number of ways
probability = number_of_ways/6**N
print(probability)