5

Not super sure this is the right *exchange for this question, but here we go.

Let's say I'm writing a game, and in this game the player may attack another unit. The chance of hitting is an "opposed roll" that looks something like this:

An attack lands if:

(attack * random() + attack * random())
is greater than or equal to
(defense * random() + defense * random())

Where attack and defense are both numbers > 0 and random() returns a (pseudo) random number between 0 and 1 (not including 1). The reason it looks like it does is to ensure that there is a bias towards the "average value" (or rather, towards double the average value, but it doesn't really matter).

How would one go about calculating the "hit chance" for this algorithm (in order to display it to the user)?

Thanks!

  • http://math.stackexchange.com/questions/357672/density-of-sum-of-two-uniform-random-variables-0-1 should get you started, I think. And yes, this is the right place to ask this question, although it is not asked in the usual mathematical language. – dfeuer Aug 08 '13 at 00:49
  • What you're looking at, by the way, are called "independent, identical, uniformly distributed random variables". You're calculating the probability that a linear combination of four such is greater than $0$. – dfeuer Aug 08 '13 at 00:54

1 Answers1

2

The attack is successful iff $$Ax_1+Ax_2\ge Dy_1+Dy_2\iff A(x_1+x_2)\ge D(y_1+y_2)\iff x\ge \frac{D}{A}y,$$ where $x_1,x_2,y_1,y_2$ are realizations of the $U[0,1]$ random variables and $x,y$ are the respective sums. Now, using the density for sums of two $U[0,1]$ random variables (see the comment above), consider the case $2\ge D/A\ge 1$: $$\operatorname{Pr}\left(x\ge \frac{D}{A}y\right)=\int_{D/Ay}^1 \int_0^{\underline{y}} xy \,\mathrm{d}x\,\mathrm{d}y+\int_1^2 \int_0^{\underline{y}} (2-x)y \,\mathrm{d}x\,\mathrm{d}y+\int_{D/Ay}^2 \int_{\underline{y}}^1 (2-x)y \,\mathrm{d}x\,\mathrm{d}y+\int_{D/Ay}^2\int_1^{\overline{y}}(2-y)(2-x) \,\mathrm{d}x\,\mathrm{d}y,$$ where $\underline{y}$ is the $y$ such that the minimum $x$ beating this $y$ equals $1$. That is, $x=1=D/Ay\iff y=A/D\le 1$. Similarly, $\overline{y}$ is the $y$ such that the minimum $x$ beating that $y$ equals $2$. Thus, $x=2=D/Ay\iff y=2A/D\le 2$. The integrals represent all cases where the attack is successful, and we need to distinguish the intervals $[0,1]$ and $[1,2]$, because the sums $x,y$ have different densities in these intervals. We only considered the case $2\ge D/A\ge 1$, because for any other case the integral borders or cases will change slightly.

Solving the integrals explicitly (not yet evaluating), we get $$[\tfrac14x^2y^2]+[xy^2-\tfrac14x^2y^2]+[xy^2-\tfrac14x^2y^2]+[4yx-x^2y-y^2x+\tfrac14x^2y^2]$$ Now one just has to plug in the $x$ integral borders for each of the 4 terms, then the $y$ borders, and simplifying will give the result. This is a very tedious calculation, maybe it can be done quickly in some symbolic math tool.

Nameless
  • 4,045
  • 2
  • 20
  • 36
  • I don't understand. If $\underline y$ is a set, how can it be used as a limit of integration? – dfeuer Aug 08 '13 at 17:03
  • It's a singleton (but you're right I write it like a set - how to write this instead?). The idea is that we need to find the $y$ for which (the other sum) $x$ is equal to 1, because for all larger $y$ the winning sum $x$ must be greater than 1, where it has another density. – Nameless Aug 08 '13 at 19:01
  • A way I would not recommend that is valid in a universe of pure sets but most confusing here is to write it as the union of a singleton. Better, probably, is just to describe it in words. – dfeuer Aug 09 '13 at 00:33