drawing random number from a standard normal distribution using the standard rand() functions

柔情痞子 提交于 2019-11-28 12:56:34

问题


So most programming languages contain a rand() functionality whereby you can generate a random number between 0 to 1....

My question is, is there a way to manipulate this standard rand() functionality such that you are able to instead draw a random number from normal distribution with a mean and standard deviation?

Notice that I'm not asking whether or not a language has a normal distribution functionality built in, I'm asking if you can "simulate" drawing a normal distribution random variable using a rand() function...


回答1:


Here is a simple way that works (from here):

    static Random r = new Random();
    static double Normal(double mu, double sig)
    {
        double u, v, x, y, q;
        do
        {
            u = r.NextDouble();
            v = 1.7156 * (r.NextDouble() - 0.5);
            x = u - 0.449871;
            y = Math.Abs(v) + 0.386595;
            q = Math.Sqrt(x) + y * (0.19600 * y - 0.25472 * x);
        } while (q > 0.27597 && (q > 0.27846 || Math.Sqrt(v) > -4.0 * Math.Log(u) * Math.Sqrt(u)));
        return mu + sig * v / u;
    }


来源:https://stackoverflow.com/questions/8739851/drawing-random-number-from-a-standard-normal-distribution-using-the-standard-ran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!