# What is the probability of given attack being greater than or equal to given defense plus random variable

Imagine a game where an encounter between player and cpu plays like this:

Player has pattack stat, cpu has cdefense stat, and then there's a uniformly random generated variable rnd that, let's say, can have the values in the range -0.2 to +0.2.

The logic is easy:

if(pattack >= (cdefense + rnd))
{
player wins
}


Knowing pattack and cdefense I'd like to know the probability (0,1) that player will win.

I've been looking at conditional probability, but failed to understand how to apply the fact that stats are "constant in the moment that I want to calculate the probability" but they can have any value prior to that moment.

• Let's denote the lower limit of the (uniformly) random generated variable rnd as $a$ and the upper limit as $b$ with $b>a,a<0,b>0$ (in your case, $a=−0.2,b=0.2$). If $\text{pattack}$ is within the interval of $(\text{cdefense}−|a|,\text{cdefense}+b)$, then the probability that the player wins is simply: $P(Win)=(pattack−(cdefense−|a|))/(b−a)$. If $\text{pattack}$ is lower than the lower bound of the interval, then the player will never win and if it is above the upper bound of the interval the player always wins. – COOLSerdash Jul 9 '13 at 22:03
• I presume you can already work out $P(\text{rnd} \leq x)$ for any $x$. Just rearrange the expression like so: $P(\text{pattack} \geq \text{cdefense} + \text{rnd})$ $=$ $P(\text{rnd} \leq \text{pattack} - \text{cdefense})$. – Glen_b Jul 10 '13 at 0:21

If you didn't know the values of pattack and cdefense, then you would have to consider the variation in three random variables rather than just one, and the probability would not be conditional. As it is, pattack and cdefense are fixed, and so you only need to consider the variation in rnd, and the probability is conditional.

Take a more familiar example.

• If you roll two dice, you need to account for the randomness in each die to see how likely each total score is. Here, 2 and 12 are very unlikely, and 7 is most likely.
• If however you roll two dice and one falls off the table onto the floor and you only see the die on the table, then you only need to account for the randomness in the die on the floor because the value of the die on the table is given. I.e. you calculate the probability of the total conditional on the value of the die on the table. Let's say the value of the die on the table is 6. Then totals of 2 to 6 are impossible (probability zero), and totals 7 to 12 are equally likely (probability 1/6).

To solve your particular problem, first rearrange the inequality so that rnd is the subject: (pattack - cdefense) >= rnd. And assume that rnd is in the range -0.2 to +0.2 as you said, and any value of rnd in this range is equally likely (this is known as a uniformly distributed random variable).

Clearly if (pattack - cdefense) >= 0.2, then the probability P((pattack - cdefense) >= rnd) = 1, i.e. certain. Conversely if (pattack - cdefense) <= -0.2, then the probability P((pattack - cdefense) >= rnd) = 0, i.e. impossible.

Otherwise,

P((pattack - cdefense) >= rnd) = $\frac{(pattack - cdefense) - (-0.2)}{0.4}$

Here the numerator is how far through the possible range of rnd the difference $(pattack - cdefense)$ is, and the denominator is the total range of rnd (i.e. $0.4 = 0.2 - (-0.2)$). To see the logic behind this, work through an example. Imagine pattack = 0.8, pdefense = 0.7, so (pattack - cdefense) = 0.1, which is three quarters of the way through the range -0.2 to +0.2. The formula above gives $\frac{0.1 - (-0.2)}{0.4} = \frac{0.3}{0.4}$ = 0.75, or 75%.

This can be generalised for other ranges of rnd.