Guye Incognito - 2 months ago 34

C# Question

I'd like to be able to generate a bell curve to use for probability in procedural generation in games.

For, example if I want to generate a forest, I could give it a radius and intensity and make a bell curve of the right shape to give me the probability of whether a tree should be placed or not. I should end up with a lot of trees in the centre and they would become less frequent as you approach the radius distance out from the centre.

I've done something similar before using a sine wave. But I have no idea how to make a bell curve. These equations are greek to me. I have forgotten how to *read* them, but it would be greek to the computer anyway.

Could someone write down the equation for bell curve in C# (or python would be my 2nd choice) and maybe explain it a bit?

Answer

Sure.

```
p(x) = exp(-(x-mu)^2/(2*sigma^2))/sqrt(2*pi*sigma^2)
```

The bell curve is also called the gaussian probability distribution. It is basically taking *e* to the power of a negative square of the *x* value. The rest is to make sure that it is centered at mu and scaled to the specifics of the particular problem you are modeling, and to make sure that the integral over all values of *x* sums to 1.

To generate random samples from this distribution in Python, you can do something like the following:

```
import random
sample = random.gauss(mu, sigma)
# where mu is the center of the bell curve, and sigma is proportional to its "width"
```

If you want to have a two-dimensional bell curve, it's nice to know that you can find *X* and *Y* values separately like in the above, and the 2D plot will be a 2D bell curve, where the density is highest at the center `(mu_x, mu_y)`

.