# Random Shots and percentages behind it.

Just to be clear I am rather bad at math, however I am making a webservice and need to understand the math behind it in order to recreate it in javascript.

So here goes.I am trying calculate the percentages of a multiple random shot scenario, where the maximum amount of targets is 8 and minimum amount of targets is 1.

Each target can handle X-amount of shots and if the amount is reached the target will disappear (Which makes the whole thing hard to calculate).

A scenario

Target-A = 5 HP

Target-B = 3 HP

Target-C = 1 HP

Target-D = 1 HP

Shots to Fire = 3

The shots fire at random but will always hit a target. The shots can hit the same time multiple times but the target will be removed once it HP reaches 0.

What are the chances for Target-B to be hit 2-times in a row? and how does the math behind it look?

I am necessarily not asking you to do the math for me, I am asking for guidance in how to to understand the math required to solve my litte puzzle.

Hope you can aid me on my way.

## 1 Answer

When you say at random I'm assuming you mean each target has the same probability to be hit and other then the change in the number of targets there is no dependency between the probabilities. (In programming terms you do something like call rand(4) for each shot to determine which one gets hit and discard hits to targets that (assuming rand(n) returns and integer between 1 and n inclusive).

In that case first consider what is the probability that target $B$ is hit by the first shot, then the second shot (knowing the first shot hit B) and then the third shot (knowing the first two shots hit B). Consider each of these separately. Then if they all have to happen you can just multiply the probabilities.

Edit to answer more fully:

In case you want to actually look at the whole picture it will probably be easiest to draw a tree. This might not be feasible for more complex problems but in this case it is the easiest approach I think. You could draw a full state tree but if you only want some specific probabilities you can just draw the bits important for your case.

Let's first look at the situation where you want exactly two consecutive hits of $B$.

Since we only have 3 shots this either means the first two hit $B$ or the second two hit $B$. We will write $X_1$ through $X_3$ for the three random variables (which are not independent though).

• case 1: the probability the first one hits $B$ is $1/4$ we write $P(X_1=B)=\frac{1}{4}$ The probability the second one hits $B$ given that the first one hit $B$ is $P(X_2=B\;|\;X_1=B)=\frac{1}{4}$. We are not done yet though. Since we want exactly two consecutive hits the last must not hit $B$. Now the probability that the last one doesn't hit $B$ given that the first two did is $P(X_3\neq B\;|\;X_1=B \wedge X_2=B)=\frac{3}{4}$. So all together the probability of the first two shots being $B$ is $\frac{1}{4}\frac{1}{4}\frac{3}{4}=\frac{3}{64}$.
• case 2: This will be trickier. This time the first one is not allowed to be $B$ (which is easy) $P(X_1\neq B)=\frac{3}{4}$, but which one the first one is will affect the second two probabilities. We are lucky though since if $B$ must be hit twice only one of the targets $C$, $D$ will get hit and they look the same so we get only 2 cases.

• case 2.1: The first one hits $A$, $P(X_1=A)=\frac{1}{4}$. Then the second two must hit $B$ and both of them have the same probability since in both cases 4 targets are still left so we get $P(X_2=B\;|\; X_1=A)=P(X_3=B\;|\; X_2=B\wedge X_1=A)=\frac{1}{4}$. So all together we get $\frac{1}{4}\frac{1}{4}\frac{1}{4}=\frac{1}{64}$.
• case 2.2: The first one hits either $C$ or $D$, $P(X_1=C\vee X_1=D)=\frac{1}{2}$. Now the next two are again the same, but different from before. We get $P(X_2=B\;|\;X_1=C\vee X_1=D)=P(X_3=B\;|\;(X_1=C\vee X_1=D)\wedge X_2=B)=\frac{1}{3}$. So again all together we get $\frac{1}{2}\frac{1}{3}\frac{1}{3}=\frac{1}{18}$

Now we have to look at all three of the situations together since they are each different possibilities we now add them up $\frac{3}{64}+\frac{1}{64}+\frac{1}{18}=\frac{17}{144}$. If we didn't specify they have to be consecutive we would have to deal with 2 more cases hits of the type $BCB,BDB,BAB$.

Remark: If you are programming anyhow it might be worthwhile to just do a brute force enumeration of the state space with a probability for each element. That's quite easy to do since the probability at any point is just one over however many targets are left. Then if you want to know the probability of any given combination you just search the generated results and add up all the probabilities for matching states.

• Yes your assumption is correct the Random number is based on af random number between 1 and the amount of targets. – user224041 Mar 16 '15 at 14:54
• I will just change my question from being hit three times to two :) – user224041 Mar 16 '15 at 14:55
• I will update the answer and explain the math more fully in the evening then.:) – DRF Mar 16 '15 at 14:58
• Thank you now I finally understand it :) – user224041 Mar 18 '15 at 9:35