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.

  • 1
    \$\begingroup\$ Are you only asking how to model this in anydice or are you interested in the actual mathematical solution? \$\endgroup\$ Dec 2, 2021 at 13:19
  • \$\begingroup\$ Both, if possible, but primarily the anydice of it \$\endgroup\$ Dec 2, 2021 at 13:38
  • \$\begingroup\$ I realize now that I wish this question explained why AnyDice is a necessary part of the solution; not knowing (coupled with the comment above to ThomasMarkov that OP would be interested in the underlying mathematics) makes it hard for me to vote on the answer which uses a Python library. \$\endgroup\$
    – nitsua60
    Jan 9, 2022 at 1:12

6 Answers 6


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).


For the mathematical solution, let \$X_n\$ be the random variable for how many uses you get with \$n\$ charges.

$$E[X_n] = 1+\frac{n-1}{6}\cdot E[X_n]+\frac{7-n}{6}\cdot E[X_{n-1}]$$

$$E[X_0] = 0$$

This gives a recursive formula that allows us to calculate for each n=1,2,3,4,5,6.

I'm using the linearity of expectation to generate the formula. The recursive formula can be written as follows to make a little more sense:

$$E[X_n] = 1 + P(\text{no charge used})E[X_n] + P(\text{one charge used})E[X_{n-1}]$$

What this means is, if you use the item, the expected number of uses will be at least 1 (because you just used it). But, if the item did not reduce charges, the expected number of remaining uses is unchanged. If it did reduce the number of charges, then you add the expected number of uses with one fewer charges.

The final outcomes are:

$$E[X_0] = 0, E[X_1] = 1, E[X_2] = \dfrac{11}{5}, E[X_3] = \dfrac{37}{10}, \\ E[X_4] = \dfrac{57}{10}, E[X_5] = \dfrac{87}{10}, E[X_6] = \dfrac{147}{10}$$

Here is an example of a calculation. Assume you have already found \$E[X_1]\$ and you want to calculate \$E[X_2]\$:

$$E[X_2] = 1 + \frac{2-1}{6}\cdot E[X_2] + \frac{7-2}{6}\cdot E[X_1] = 1 + \frac{1}{6}\cdot E[X_2] + \frac{5}{6}\cdot 1$$

$$\frac{5}{6}E[X_2] = \frac{11}{6}$$

$$E[X_2] = \frac{11}{5}$$

  • \$\begingroup\$ So ~14 uses before remaining charges becomes zero? \$\endgroup\$ Dec 2, 2021 at 17:34
  • 1
    \$\begingroup\$ @MichaelRichardson: On average, yes. The variance is huge, though. Based on Hypergardens' AnyDice program you have a ~95% chance (same as rolling 2+ on a d20) of getting 8 or more uses, and a ~5% chance (same as rolling a nat 20) of getting 27 or more uses. It's not a symmetric distribution, either, so the most likely number of uses (i.e. mode) ends up being 11, while the median is 13 and the mean ~14.6. \$\endgroup\$ Dec 2, 2021 at 19:25
  • \$\begingroup\$ I found this reasoning a little bit complicated, even if it looks like correct. Anyway, observing that \$1-P(\mbox{no charge used})=P(\mbox{one charge used})\$, collecting \$E[X_n]\$ in the lhs of the equation and diving both side by \$P(\mbox{one charge used})\$, you obtain the same recursive relation based on the geometric distribution \$\endgroup\$
    – Eddymage
    Dec 3, 2021 at 7:26

Is this the correct solution?

set "explode depth" to 20
set "maximum function depth" to 20

function: left TN:n {
  if TN <= 1 {
    result: 1
  } else {
    result: 1 + [explode d6 < (TN)] + [left (TN-1)]

output [left 6] named "6 uses"
output [left 5] named "5 uses"
output [left 4] named "4 uses"
output [left 3] named "3 uses"
output [left 2] named "2 uses"
output [left 1] named "1 uses"


  • \$\begingroup\$ +1, looks good to me. To pick a few minor nits, your explode depth is arguably a bit low for [left 6], and you don't actually need to increase the maximum function depth above the default of 10. Also, for plotting long-tailed distributions like this, it may be convenient to introduce an arbitrary cutoff. I tweaked your script a bit to show what I mean. Still, very nice answer. :) \$\endgroup\$ Dec 2, 2021 at 18:29

Given \$m\$ charges left, the expected number of uses is \$\displaystyle 6\cdot\sum_{i=1}^m\frac{1}{7-i}\$


For the purpose of this question, we will be assuming that using this item does not automatically consume a charge, while acknowledging there is still a chance this happen.

Let's start with a simple example, where you have 4 charges left: then, you spend one charge when you roll 4 or higher on a d6, and such event has a probability of \$3/6=1/2\$. The probability distribution is a geometric one, as noted in Ilmari's answer, hence the expected number of trails before you get one success is the inverse of the probability: in this case, this number is 2.

Once you roll a 4, then you have 3 charges left, and you spend one once you roll a 3 or higher on the d6, which has a probability of \$4/6=2/3\$. The expected trails before getting a 3 is hence 1.5.

You are left with 2 charges: you expend one charge as soon as you roll 2 or higher, with probability \$5/6\$, resulting in an expected number of trails of 1.2. Finally, when you are left with only one charge, as soon as you use the item you spend this last charge.

The expected number of trails before spending all the 4 charges is hence 2+1.5+1.2+1=5.7.

We can get then the final formula: given \$m\$ charges left and assuming that \$m\leq 6\$, then the expected number \$N_m\$ of trails before finishing all the charges is $$ {\rm E}[N_m] = \left(\frac{7-m}{6}\right)^{-1}+\left(\frac{7-(m-1)}{6}\right)^{-1}+\left(\frac{7-(m-2)}{6}\right)^{-1}+\dots+\left(\frac{1}{6}\right)^{-1} $$ where the first term accounts for the number of trails when \$m\$ charges are left, the second term accounts for the number of trails when \$m-1\$ charges are left and so on.

We can write a more compact formula: $$ {\rm E}[N_m] = \sum_{i=1}^m\left(\frac{7-i}{6}\right)^{-1} = 6\sum_{i=1}^m\frac{1}{7-i} $$

Down below the exact values for \$m=1,\dots,6\$.

$$ \begin{eqnarray} {\rm E}[N_1] &=& 1\\ {\rm E}[N_2] &=& 2.2\\ {\rm E}[N_3] &=& 3.7\\ {\rm E}[N_4] &=& 5.7\\ {\rm E}[N_5] &=& 8.7\\ {\rm E}[N_6] &=& 14.7\\ \end{eqnarray} $$


The number of times you can use the item before losing a charge is a random variable with a geometric distribution, which we will call \$G_n\$.

The total number of times you can use it before using all charges is therefore:


So that’s your answer.

If you want to know the expected value (i.e. number of uses remaining), since we know the expected value of \$G_n\$ is \$1\over p\$, where \$p\$ is the chance of rolling over the current number of charges \$T_n\$. Due to the linearity of expectation we can add up all of the the individual expectations to get values for subsequent expectation values. So, calling the expected remaining uses \$E(T_n)\$:

$$E(T_1)={6\over 6}=1$$

$$E(T_2)={6\over 5}+{6\over 6} = {11\over 5} = 2.2$$

$$E(T_3)={6\over 4}+ {11\over 5} = {37 \over 10} = 3.7$$

$$E(T_4)={6\over 3} + {37\over 10} = {57 \over 10} = 5.7$$

$$E(T_5)={6\over 2} + {57\over 10} = {87 \over 10} = 8.7$$

$$E(T_6)={6\over 1} + {87\over 10} = {147 \over 10} = 14.7$$


This is not an AnyDice-based solution, but is otherwise directly responsive and also tackles a limitation of AnyDice surfaced by other answers of this question. I.e.: How do I get the precision I want? With AnyDice, you have to translate precision requirements to explosion or recursion depth, which usually involves experimenting to where the results (at least to the hundredth place) don't change (much) without hitting timeout limits.

This inspired an experimental feature I developed for dyce¹. Specifically, during substitution (including explosion), dyce has the ability to halt recursion at a “precision”-based threshold, rather than a depth-based one. The precise definition is (still) a bit complicated, but it's a work-in-progress. (Feedback is welcome!)

The idea is to tell dyce to keep recursing until the impact of any further expansion would be at or below a given precision threshold, but otherwise let it figure out where that occurs. The goal is to make a more ergonomic interface for those who think in terms of precision tolerances rather than something as esoteric as recursion depth.

You can play around with a more generalized version in your browser: Try dyce [source]

While a matter of taste, I find anydyce's² "burst" graphs are well-suited to visualizing distributions for this mechanic. (Screenshot below.)

¹ dyce is my Python dice probability library.

² anydyce is my visualization layer for dyce meant as a rough stand-in for AnyDice.

anydyce burst graphs


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .