# dnd 5e – Analyzing a homebrew casting mechanism

Let $$p_i$$ be the probability of the $$i$$-th check succeeding, and let $$N$$ be a random variable describing the number of successful casting attempts. Then the probability of successfully casting the spell at least $$k$$ times is: $$mathrm P(N ≥ k) = p_1 times p_2 times cdots times p_k = prod_{i=1}^k p_i,$$ where the $$Pi$$ symbol denotes multiplying together $$p_i$$ for values of all $$i$$ from $$1$$ to $$k$$.

Knowing these probabilities for all $$k$$ is enough to fully characterize the distribution of $$N$$. In particular, it lets us calculate the probability of successfully casting the spell exactly $$k$$ times: $$mathrm P(N = k) = mathrm P(N ≥ k) – mathrm P(N ≥ k+1) = (1-p_{k+1}) prod_{i=1}^k p_i.$$ (This should make sense when you think about it: it’s just the probability of succeeding on the first $$k$$ checks and the failing the next one.)

In particular, we can also calculate the expected number of successful casts using either of the following formulas: begin{aligned} mathbb E(N) &= sum_{k=1}^infty k times mathrm P(N = k) \ mathbb E(N) &= sum_{k=1}^infty P(N ≥ k) end{aligned}

These formulas can be fairly easily applied by hand: first calculate a table of the single-roll success probabilities $$p_i$$, keeping in mind the changing die size. Then compile another table of the cumulative probabilities $$mathrm P(N ≥ k)$$ of succeeding on the first $$k$$ rolls (i.e. the product of the first $$k$$ items in the first table), and finally sum the numbers in the second table to obtain the expectation value $$mathbb E(N)$$.

Or you can just make a spreadsheet to do it for you.

(Note: the linked spreadsheet is read only. To test it for different spell levels, make a copy first.)

Alternatively, we can calculate the probabilities “backwards” using the following recurrence: begin{aligned} N_{k-1} &= begin{cases} 1 + N_k & text{with probability }p_k \ 0 & text{with probability }1-p_k end{cases} \ &= mathbf 1_{p_k} (1 + N_k),end{aligned} where $$N_k$$ is a random variable denoting the number of successful casting attempts after $$k$$ successes, and $$mathbf 1_{p_k}$$ is a random variable that equals $$1$$ with probability $$p_k$$ and $$0$$ otherwise.

Using this formula, it’s quite easy to calculate the distribution of $$N$$ iteratively using AnyDice and plot the results:

``````DICE: {20, 12, 10, 8, 6, 4:15}

loop LEVEL over {1..9} {
SPELLS: 0
loop X over (reverse DICE) {
SPELLS: (dX > 2*LEVEL) * (1 + SPELLS)
}
output SPELLS named "spell level (LEVEL)"
}
``````

Of course, to apply this “backwards” formula, we have to start with an approximation of $$N_k$$ for some sufficiently large value of $$k$$. The code above effectively assumes that the player will never succeed on more than fifteen d4 rolls (i.e. no more than 20 rolls total), and thus starts with $$N_{20} = 0$$. (You can change this limit by changing the length of the `DICE` array.)

For spell level 2 and above this assumption is exact (and in fact overkill), since a d4 roll is guaranteed to fail; for level 1 spells it’s not, but in practice the probability of succeeding on 15 successive checks with a 50% success rate each is negligible ($$2^{-15} ≈ 0.00003$$), so it’s a very good approximation.