gauss

Gaussian Distribution


Figure copyright © 2007, Petter Strandmark

This script returns a pseudo-random number with Gaussian or normal distribution, meaning the values returned tend to cluster around a given average or mean value. This could be useful if one needed a random behavior or outcome where deviations from the desired target become more rare as the magnitude of the deviation increases.

An example use might be an enemy AI whose skill is represented by the degree of error in its aiming ability. If a target is at 90 degrees, aim = gauss(90,5) would return a firing direction with a small degree of error. This degree of error (or deviation) is controlled by the second argument. In this case, the value returned would be within 5 degrees (one standard deviation) of the desired direction (the mean) about 68% of the time, and within 10 degrees (two standard deviations) about 95% of the time. The lower the given deviation, the greater chance the returned value will be near the given mean, and the more accurate the aim of the AI would be.

From the Wikipedia entry on normal distribution:

In probability theory, the normal (or Gaussian) distribution is a continuous probability distribution that is often used as a first approximation to describe real-valued random variables that tend to cluster around a single mean value. The graph of the associated probability density function is "bell"-shaped, and is known as the Gaussian function or bell curve:

where parameter is the mean (location of the peak) and s 2 is the variance (the measure of the width of the distribution). The distribution with = 0 and s 2 = 1 is called the standard normal.

! NOTE: This replaces an older script of the same name. This version is both faster and more accurate. One can use gauss(0,1.66667) to closely approximate the output of the older script.

Downloadgauss(mean, deviation)   Returns a pseudo-random number with a Gaussian distribution.
/*
**  Usage:
**      gauss(mean, deviation)
**
**  Arguments:
**      mean        mean value of the distribution
**      deviation   standard deviation of distribution
**
**  Returns:
**      a pseudo-random number with an exact Gaussian distribution
**
**  Notes:
**      See http://en.wikipedia.org/wiki/Normal_distribution
**      for more information about Gaussian distributions
**
**  GMLscripts.com
*/

{
    var x1, x2, w;

    do {
        x1 = random(2) - 1;
        x2 = random(2) - 1;
        w = x1*x1 + x2*x2;
    } until (0 < w && w < 1);

    w = sqrt(-2 * ln(w)/w);
    return argument0 + argument1 * x1 * w;
}

Click if you've used this script[Please Login]
Projects: 6

 Contributor: Yourself

 History:
Oct 18, 2007 - Yourself submits the original exact_gauss() script
Jul 24, 2011 - xot replaces the existing gauss() script with this one


comments powered by Disqus