# Calculating "Spread" for shotgun shot

This topic is 2947 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I am working on Programming Game AI by Example and the author calculates spread for shotgun pellets(one shotgun shell has more pellets) like this:

C# code:

 //determine deviation from target using a bell curve type distribution double deviation = Functions.RandInRange(0, Spread) + Functions.RandInRange(0, Spread) - Spread; // Vector from shooter to target Vector adjustedTarget = pos - owner.Position; //rotate the target vector by the deviation Vector.RotateVector2DAroundOrigin(ref adjustedTarget, deviation); //add a pellet to the game world owner.World.AddShotGunPellet(owner, adjustedTarget + owner.Position); 

Why deviation has bell curve distribution?

Thank you

##### Share on other sites
Why not? Intuitively, and experimentally, buck shot spreads out as it travels. The exact position of each pellet each time you fire will essentially be random (at least locally). However, the overall statistical behavior can be predicted and can be easily modeled as a Gaussian distribution in 2 dimensions. I imagine this was their motivation.

##### Share on other sites
The resulting distribution is not Gaussian (aka "bell curve"). It does, however, approximate a Gaussian, roughly.

Here's why:

Addition of two independent random variables is equivalent to convolution of their probability distributions. And that's exactly the situation you have here: The variable "deviation" that you have is computed in your code as the sum of two independent random variables. Each of these random variables' probability distributions is a rectangle. When you convolve two rectangles, you get a triangle (you can work this out graphically). So you really are sampling deviations from a triangular distribution. If you were to add successively more random variables to your sum, you'd get successively better approximations to a bell curve. That's the essential message of the "Central Limit Theorem." In the case of adding up many uniform random variables, it can be understood intuitively by remembering that convolution with a rectangle is just a box blur (so in photoshop, a whole bunch of box blurs is mathematically almost the same as a single Gaussian blur). It's also why Gaussian distributions are observed so often in nature: Any time you have a great many independent random "things" that can affect the outcome, and those "things" combine additively, the outcome is going to be Gaussian.

In order to understand it, I'd encourage you to work out the joint probabilities for the discrete case. Say you have two independent, uniform random variables over the set of integers from -5 to 5, inclusive. Write out the 11x11 table of possible combinations, and work out how often each possible sum occurs. If you plot this histogram, you'll see the triangular shape I'm talking about.

This is the same reason why, in games that make you roll two dice, you get "lucky sevens;" I'd encourage you to do that table as well; it's the same thing, just shifted.

Hope that's enough to get started.

##### Share on other sites
This would achieve the same effect in a slightly simpler way:
 //determine deviation from target using a bell curve type distribution double deviation = Functions.RandInRange(0, Spread) - Functions.RandInRange(0, Spread);

##### Share on other sites
I think the pellets will vary in angle fairly uniformly. It's the resultant of many pellets that has a normal distribution. Therefore the author is misunderstanding statistics.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 12
• 9
• 11
• 15