I want to know how can be expressed mathematically (if at all!) a certain game mechanic that produces a single number by rolling several dice and making decisions based on the results. I'm lost at how to express it, as it is not a simple linear combination or condition. My intent is to understand well the mathematical expression of the algorithm and to know if it's possible to compute more effectively (by hand and with software) values like expected value and variance, or if it's necessary to just find out the Probability Mass Function for the whole system, and calculate from there.
The mechanic is an attack sequence similar to many RPG games, where a first die roll determines success or failure in an attack, and success means rolling more dice to find how much damage was dealt. It is a tiny bit more involved than typical D&D, but here is a simplified example with some fixed values for simplicity:
- Roll a $20$ sided die, and if the value is $5$ or lower is a miss, but still produces $3$ damage. No need to roll anything more.
- If the previous roll was $6$ or higher it was a hit. Roll a standard $6$ side die, and if a $5$ or $6$ is rolled it was a traumatic hit, otherwise it was a normal hit.
- Roll a $10$ sided die for the amount of damage dealt. If a traumatic hit was rolled before, the damage is multiplied by $3$. Otherwise is just the amount just rolled.
It is reasonably easy to make the PMF for this:
- $25\%$ of chance of failure, which produces a result of $3$.
- 50% of regular hit, which will produce the results from $1$ to $10$ with 5% chance each.
- $25\%$ of the trauma hit, which again is split in the results $3, 6, 9\ldots$ up to $30$ (because it's $1, 2, 3,$ etc. multiplied by $3$), each with $2.5\%$ chance.
Just adding the probabilities of the different outcomes would work, if I'm not mistaken, and from the Probability Mass Function we can get variance and mean, etc.
But since each die individually is much simpler and (mean and variance can be easily obtained), I would like to know if an algorithm like this can be expressed as some sort of combination of its parts in a way that could be simpler and more understandable.
EDIT: To summarize and clarify, I'd like to know if we can write that algorithm as a single random variable $X$ which is a function of the other random variables, using conditionals and linear combinations: the "to hit" roll $H$, the trauma roll $T$ and the damage roll $D$. Something like
$$ X = p_{H \le 5} · 3 + p_{H \ge 6} · D ( p_{T \le 4 } · 1 + p_{T \ge 5} · 3) $$
The above doesn't make entire sense, but it's along the lines of what I was trying to (unsuccessfully) express. If that were possible, I could use for example the propagation formulas for variance to calculate the variance based on the variance of the individual dice rolls. Likewise for expected value, etc. I would not be surprised if this isn't possible, but I need to ask. :-)
Thank you.
At best, your proposed expression for $X$ is highly unusual; I don't think I have ever seen the letter $p$ used this way. Usually $p$ (with or without a subscript) in this context would represent a probability, which is simply a number, not a random variable.
I think some form of Iverson brackets would best provide what you are looking for:
$$ [P] = \begin{cases} 1 & \text{if } P \text{ is true;} \\ 0 & \text{otherwise}. \end{cases} $$
Using this notation,
$$ X = [H \leq 5]\cdot 3 + [H \geq 6]\cdot D( [T \leq 4]\cdot 1 + [T \geq 5]\cdot 3).$$
Equivalently,
$$ X = [H \leq 5]\cdot 3 + [H \geq 6 \land T \leq 4]\cdot D + [H \geq 6 \land T \geq 5]\cdot 3D. $$
Note that you could get the same distribution (but perhaps have less fun rolling for damage) if you let
$$ X = [H \leq 5]\cdot 3 + [6 \leq H \leq 15]\cdot D + [H \geq 16]\cdot 3D. $$
Each of these Iverson brackets is a Bernoulli variable, so the expectation is easily computed. For variance, you could use the formula $\operatorname{Var}(X) = \mathbb E(X^2) - (\mathbb EX)^2,$ where
$$ X^2 = [H \leq 5]\cdot 9 + [H \geq 6 \land T \leq 4]\cdot D^2 + [H \geq 6 \land T \geq 5]\cdot 9D^2. $$
You can also use moments of $X$ (expected values of powers of $X$) to compute kurtosis when you want to.