[RPG] Modelling “rolling d6 >= charges left expends a charge” mechanics with anydice


You have X charges left. Rolling a d6 equal or over the number of charges left depletes a charge. How many uses do you get out of a starting X charges?

e.g. 1 charge, 1 use left. 2 charges, rolling 2 or more depletes a charge.

Best Answer

Here's an alternative, more "procedural" AnyDice solution:

set "explode depth" to 50

loop K over {1..6} {
  USES: USES + 1 + [explode d6 < K]
  output [lowest of 50 and USES] named "[K] charges"

It relies on the same recurrence as Hypergardens' answer, i.e. that $$N_k - N_{k-1} = 1 + X_k,$$ where the random variable \$N_k\$ denotes the number of uses with \$k\$ charges left, and \$X_k\$ is a (zero-based) geometrically distributed random variable with parameter \$p = \frac{k-1}6\$ (approximated in AnyDice with [explode d6 < K]) denoting the number of uses before the current charge will be expended. The difference is that, whereas Hypergardens' solution computes \$N_k\$ on demand for any given \$k\$ using a recursive function, my code starts with with \$N_0 = 0\$ and iteratively computes \$N_k\$ for successive values of \$k\$.

Which program is better? Honestly, that's mostly a matter of taste. My solution is likely to be slightly faster, at least if you want to compute \$N_k\$ for all values of \$k\$ in one run, but in practice both are more than fast enough, and Hypergardens' recursive solution is arguably more flexible. In any case, the both give the same results (at least if you use the same explode depth and output cutoff).

Related Topic