# Speedrun Probability Math in Boss Fight Video Games

Context: Speedrunning is a way of playing a video game with the aim of completing it as fast as possible. This probability question was inspired by a very simple video game's boss battle, and it is a math question at its core. I tried calculating this myself but made several mistakes and got lost somewhere at n = 2. (Out of curiousity, I also asked some chatty AI friends, and some of them spit out probabilities that added up to more than 1... which from what I remember is a no-no.)

Let's say we have a Boss enemy who only has two meaningful attacks:

• Open (O), an attack that allows the Player to hit the Boss. The Boss chooses this attack 53% of the time.
• Closed (C), an attack that does not allow a hit. The Boss chooses this attack 47% of the time.

We're going to assume our player can land the hit on an Open attack every single time with trivial difficulty. The boss has 5 health, meaning once they use their 5th Open attack, the player wins and the fight ends.

What I couldn't figure out were the formula for the probabilities of getting exactly n number of closed attacks (ideally from 0-10, since anything more than 10 is too slow to continue), as well as what the most likely pattern would be, average pattern (50th percentile result, maybe that's different from the most common?), and what the 80th percentile of luck would look like (not exactly sure whether that should be 80th or 20th, but I mean how many C attacks is so lucky that 80% of attempts would have that many Cs or more.

I tried doing this by hand, but I kind of fell apart trying n = 2 and 3 because I was lost multiplying in all the factors of 0.53 and 0.47, I couldn't figure out a sensible formula to speed up the calculations and I don't think I was doing it correctly anyway. My end goal was to make a table of patterns and their probabilities, but I realized I was in over my head.

As an example, I calculated that the odds of a battle where the boss uses C 0 times are:

(0.53)^5

, since there is only one way for this to happen: Boss has to roll an O 5 times consecutively, at a 53% chance each time.

For the Boss to give us a fight with exactly 1 C attack, I started by assuming the first attack was C. Thus our sequence had to be COO OOO. The probability of that exact sequence is

(0.47)(0.53)^5

, since the first attack has to be C and then everything else has to be O. Next, I figured there are 5 ways to get a 1-Closed attack fight:

COO OOO
OCO OOO
OOC OOO
OOO COO
OOO OCO


The 6th attack can't be C, since the fight would already be over after getting 5 O attacks and besides, the fight can't end on a C attack since the Player can't hit during that move. Therefore, I figured in the context of a 1-Closed attack fight, any of those 5 configurations would be equally likely, so I just multiplied that by 5 to get

5(0.47)(0.53)^5

. I think that assumption that all 5 configurations are equally likely is a fair one, since we already get to assume there's exactly 1, but I'm not sure - it's been a while since whatever math(s) course taught me this.

The number of required attacks $$X$$ is a random variable with the negative binomial distribution (carefully note that there are two versions for this distribution). Hence, the probability of winning at the $$x$$th attack is given by for $$x\ge r$$: $$\mathbb P (X=x)=\binom{x-1}{r-1}p^{r}(1-p)^{x-r}$$ where $$r=5$$ and $$p=0.53$$ ($$x=n+r$$ if $$n$$ denotes the number of required close attacks).

The expected number of required attacks $$X$$ is $$\frac r p=\frac{5}{0.53}$$, and the expected number of required close attacks $$Y$$ is $$\frac{5}{0.53}-5$$, where we have $$X=Y+r$$.

You can also obtain the $$\alpha$$ percentile of $$X$$ by numerically finding the smallest $$k$$ that satisfies the inequality: $$\sum_{x=r}^k\binom{x-1}{r-1}p^{r}(1-p)^{x-r} \ge \alpha.$$

I'm sure you can do the rest by yourself now.

• @user45266 The question you raised is very interesting and practical, while it is an application of the negative binomial distribution, as also stated by Maximilian. In the above answer, I assumed that you were familiar with the concepts of random variables and distributions (more details can be found in the provided link to an online course).
– Amir
Commented Aug 12 at 9:33
• Just note that the negative binomial distribution is used in two ways in the literature. In one case (e.g. see Ross's text book), the total number of trials until the $r$-th success is considered a random variable ($Xâ‰¥r$), while in the other case (e.g. see Wikipedia), the number of failures leading to the $r$-th success is considered a random variable ($Yâ‰¥0$). Indeed, $X=Y+r$.
– Amir
Commented Aug 12 at 9:33