WarArm team (The video game team I’m part of) is currently trying to improve the weapon’s gun experience that must be ergonomic for the player and must reflect a soldier who in war tries to aim right on target. We noticed over time that the soldier’s fire was too random in the scope of the target and the propagation of the projectiles took place in an uncontrolled manner. How to fix the problem? How to control the randomness of the bullets? Someone would just say to increase the accuracy and thus reduce the range of randomness, but so it becomes too easy for the player. We want more control of the weapon, it needs to shoot more in the center and less on the sides.

### Let me explain with pictures These 20 bullets were fired with a uniform casuality This is what we want, 20 bullets that tend to be fired more towards the center

### How to fix this problem?

My approach is to use the theory of probability and I therefore decided to help me with the wonderful gaussian function. Gaussian function appears as the density function of the normal distribution and we will use this. We must consider this function with μ = 0 and σ = “weapon accuracy“. This values transform our “normal distribution” into a “normal standard distribution” density function. We have a density that has the following graph and has mean values at zero point. Zero point will be the center of our screen, where the shots should go. If at point x, the corresponding f(x) is high then there is more chance that the shot goes to that point. By varying the “weapon accuracy” (zero is the center of the screen)

### It all seems complicated but …

This formula does not seem easy to apply. The computer fails to generate randomness of this type, because the random features available in the basic libraries of most languages are of uniform distribution. That is, uniform randomness over a range [a, b) such as the bullets distribution in the first image. Searching and searching I found a way that allows us to approximate normal distribution starting from two randomly uniformly distributed variables (which we have available with any function that generates a random number).

### Box–Muller transform

Here we are finally at the heart of the matter. Now we understand how to generate random numbers so as to simulate the will of the soldier to shoot tensely toward the center rather than at random in a spherical range. To get to a normal distribution we use the Box–Muller transform for generating standard, independent, normally distributed with zero expectation, unit variance (that we will change) random numbers, given a source of uniformly distributed random numbers.

This transformation get in input two-dimensional continuous uniform distribution to give in output a normal distribution. If x1 and x2 are our two random generated values between 0 and 1, we can use these random variables in this formula to get a normal distributed value: At the same formula can be applied the cosine

### Code example in C#

This is an example of use:

The two error variables (ErrorX and ErrorY) will be used to define the point of the shot. Firing several shots you will notice that they will tend toward the center! It will no longer be a totally random shot. I wonder if the best games on the market will use a similar technique.

What do you think of this idea? Firing probability: red very likely. Yellow unlikely

## Update

### Polar form (computationally faster)

You can avoid the sine function and apply a formula in polar coordinates that returns a normal distributed random variable.
We need the usual two uniformly distributed random variables (u and v) and a variable s = u^2 + v^2 such that 0<s≤1 (These hypotheses are important).

Now we can get two normal variables (maybe one to cache for next use) with this formula:  Obviously we will multiply these variables for variance to change the accuracy. Gianluca Mancusi

Hello visitor! I am an italian 21 year old guy who has a strong passion for computer science. To know more visit the "about me" page.

### 2 Responses

1. Brennan says:

Good article! -A roblox developer lol

• Gianluca Mancusi says:

Thank you Brennan!