5

我在使用 JavaScript 生成正态分布随机数 (mu=0 sigma=1) 时遇到问题。

我已经尝试过 Box-Muller 的方法和 ziggurat,但生成的一系列数字的平均值为 0.0015 或 -0.0018——与零相差甚远!!超过 500,000 个随机生成的数字,这是一个大问题。它应该接近于零,例如 0.000000000001。

我无法弄清楚这是否是方法问题,或者 JavaScript 的内置是否Math.random()生成不完全均匀分布的数字。

有人发现过类似的问题吗?

在这里您可以找到 ziggurat 功能:

http://www.filosophy.org/post/35/normaldistributed_random_values_in_javascript_using_the_ziggurat_algorithm/

下面是 Box-Muller 的代码:

function rnd_bmt() {
    var x = 0, y = 0, rds, c;

    // Get two random numbers from -1 to 1.
    // If the radius is zero or greater than 1, throw them out and pick two
    // new ones. Rejection sampling throws away about 20% of the pairs.
    do {
        x = Math.random()*2-1;
        y = Math.random()*2-1;
        rds = x*x + y*y;
    }
    while (rds === 0 || rds > 1) 

    // This magic is the Box-Muller Transform
    c = Math.sqrt(-2*Math.log(rds)/rds);

    // It always creates a pair of numbers. I'll return them in an array. 
    // This function is quite efficient so don't be afraid to throw one away
    // if you don't need both.
    return [x*c, y*c];
}
4

1 回答 1

5

如果您生成n独立的正态随机变量,则均值的标准差将为sigma / sqrt(n).

在您的情况下n = 500000sigma = 1平均值的标准误差约为1 / 707 = 0.0014。给定 0 均值的 95% 置信区间大约是这个或 的两倍 (-0.0028, 0.0028)。您的样本均值正好在此范围内。

您对获得0.000000000001( 1e-12) 的期望没有数学基础。要达到该准确度范围,您需要生成 about10^24样本。每秒 10,000 个样本仍需要 3 万亿年才能完成……这正是为什么尽可能避免通过模拟计算事物的原因。

另一方面,您的算法似乎确实实现了正确:)

于 2013-03-07T19:06:57.427 回答