Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a distribution of k shots on n enemies

Tags:

java

math

I am developing a space combat game in Java as part of an ongoing effort to learn the language. In a battle, I have k ships firing their guns at a fleet of n of their nefarious enemies. Depending on how many of their enemies get hit by how many of the shots, (each ship fires one shot which hits one enemy), some will be damaged and some destroyed. I want to figure out how many enemies were hit once, how many were hit twice and so on, so that at the end I have a table that looks something like this, for 100 shots fired:

Number of hits | Number of occurences | Total shots
----------------------------------------------------
       1       |        30            |      30
       2       |        12            |      24
       3       |         4            |      12
       4       |         7            |      28
       5       |         1            |       5

Obviously, I can brute force this for small numbers of shots and enemies by randomly placing each shot on an enemy and then counting how many times each got shot at the end. This method, however, will be very impractical if I've got three million intrepid heroes firing on a swarm of ten million enemies.

Ideally, what I'd like is a way to generate a distribution of how many enemies are likely to be hit by exactly some number of shots. I could then use a random number generator to pick a point on that distribution, and then repeat this process, increasing the number of hits each time, until approximately all shots are accounted for. Is there a general statistical distribution / way of estimating approximately how many enemies get hit by how many shots?

I've been trying to work out something from the birthday problem to figure out the probability of how many birthdays are shared by exactly some number of people, but have not made any significant progress.

I will be implementing this in Java.

EDIT: I found a simplification of this that may be easier to solve: what's the distribution of probabilities that n enemies are not hit at all? I.e. whats the probability that zero are not hit, one is not hit, two are not hit, etc.

It's a similar problem, (ok, the same problem but with a simplification), but seems like it might be easier to solve, and would let me generate the full distribution in a couple of iterations.

like image 777
ckersch Avatar asked May 08 '13 15:05

ckersch


1 Answers

You should take a look at multinomial distribution, constraining it to the case where all pi are equal to 1/k (be careful to note that the Wikipedia article swaps the meaning of your k and n).


Previous attempt at answer

Maybe an approach like the following will be fruitful:

  1. the probability that a particular ship is hit by a particular shot is 1/n;
  2. the probability that a given ship is hit exactly once after k shots: h1 = 1/n (1-1/n)k-1;
  3. as above, but exactly twice: h2 = (1/n)2 (1-1/n)k-2, and so on;
  4. expected number of ships hit exactly once: n h1 and so on.
like image 52
Marko Topolnik Avatar answered Oct 20 '22 12:10

Marko Topolnik