Generate a random number in a Gaussian Range?

2019-01-26 11:50发布

问题:

I want to use a random number generator that creates random numbers in a gaussian range where I can define the median by myself. I already asked a similar question here and now I'm using this code:

class RandomGaussian
    {

        private static Random random = new Random();
        private static bool haveNextNextGaussian;
        private static double nextNextGaussian;

        public static double gaussianInRange(double from, double mean, double to)
        {
            if (!(from < mean && mean < to))
                throw new ArgumentOutOfRangeException();

            int p = Convert.ToInt32(random.NextDouble() * 100);
            double retval;
            if (p < (mean * Math.Abs(from - to)))
            {
                double interval1 = (NextGaussian() * (mean - from));
                retval = from + (float)(interval1);
            }
            else
            {
                double interval2 = (NextGaussian() * (to - mean));
                retval = mean + (float)(interval2);
            }
            while (retval < from || retval > to)
            {
                if (retval < from)
                    retval = (from - retval) + from;
                if (retval > to)
                    retval = to - (retval - to);
            }
            return retval;
        }

        private static double NextGaussian()
        {
            if (haveNextNextGaussian)
            {
                haveNextNextGaussian = false;
                return nextNextGaussian;
            }
            else
            {
                double v1, v2, s;
                do
                {
                    v1 = 2 * random.NextDouble() - 1;
                    v2 = 2 * random.NextDouble() - 1;
                    s = v1 * v1 + v2 * v2;
                } while (s >= 1 || s == 0);
                double multiplier = Math.Sqrt(-2 * Math.Log(s) / s);
                nextNextGaussian = v2 * multiplier;
                haveNextNextGaussian = true;
                return v1 * multiplier;
            }
        }
    }

Then to verify the results I plotted them with gaussianInRange(0, 0.5, 1) for n=100000000

As one can see the median is really at 0.5 but there isn't really a curve visible. So what I'm doing wrong?

EDIT

What i want is something like this where I can set the highest probability by myself by passing a value.

回答1:

The simplest way to draw normal deviates conditional on them being in a particular range is with rejection sampling:

do {
    retval = NextGaussian() * stdev + mean;
} while (retval < from || to < retval);

The same sort of thing is used when you draw coordinates (v1, v2) in a circle in your unconditional normal generator.

Simply folding in values outside the range doesn't produce the same distribution.


Also, if you have a good implementation of the error function and its inverse, you can calculate the values directly using an inverse CDF. The CDF of a normal distribution is

F(retval) = (1 + erf((retval-mean) / (stdev*sqrt(2)))) / 2

The CDF of a censored distribution is

C(retval) = (F(retval) - F(from)) / (F(to) - F(from)), from ≤ x < to

To draw a random number using a CDF, you draw v from a uniform distribution on [0, 1] and solve C(retval) = v. This gives

double v = random.NextDouble();
double t1 = erf((from - mean) / (stdev*sqrt(2)));
       t2 = erf((to   - mean) / (stdev*sqrt(2)));
double retval = mean + stdev * sqrt(2) * erf_inv(t1*(1-v) + t2*v);

You can precalculate t1 and t2 for specific parameters. The advantage of this approach is that there is no rejection sampling, so you only need a single NextDouble() per draw. If the [from, to] interval is small this will be faster.


However, it sounds like you might want the binomial distribution instead.



回答2:

I have similar methods in my Graph generator (had to modify it a bit):

Returns a random floating-point number using a generator function with a specific range:

private double NextFunctional(Func<double, double> func, double from, double to, double height, out double x)
{
    double halfWidth = (to - from) / 2;
    double distance = halfWidth + from;

    x = this.rand.NextDouble() * 2 - 1;// -1 .. 1

    double y = func(x);

    x = halfWidth * x + distance;
    y *= height;

    return y;
}

Gaussian function:

private double Gauss(double x)
{
    // Graph should look better with double-x scale.
    x *= 2;

    double σ = 1 / Math.Sqrt(2 * Math.PI);
    double variance = Math.Pow(σ, 2);
    double exp = -0.5 * Math.Pow(x, 2) / variance;

    double y = 1 / Math.Sqrt(2 * Math.PI * variance) * Math.Pow(Math.E, exp);

    return y;
}

A method that generates a graph using the random numbers:

private void PlotGraph(Graphics g, Pen p, double from, double to, double height)
{
    for (int i = 0; i < 1000; i++)
    {
        double x;
        double y = this.NextFunctional(this.Gauss, from, to, height, out x);

        this.DrawPoint(g, p, x, y);
    }
}

I would rather used a cosine function - it is much faster and pretty close to the gaussian function for your needs:

double x;
double y = this.NextFunctional(a => Math.Cos(a * Math.PI), from, to, height, out x);

The out double x parameter in the NextFunctional() method is there so you can easily test it on your graphs (I use an iterator in my method).