Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
随机正态分布数字中的偏差(javascript)_Javascript_Random_Numbers_Generator - Fatal编程技术网

随机正态分布数字中的偏差(javascript)

随机正态分布数字中的偏差(javascript),javascript,random,numbers,generator,Javascript,Random,Numbers,Generator,我在生成正态分布随机数(μ=0σ=1)时遇到问题 使用JavaScript 我尝试过Box-Muller的方法和ziggurat,但生成的数字序列的平均值为0.0015或-0.0018-离零很远!!超过500000个随机生成的数字这是一个大问题。它应该接近于零,类似于0.000000000001 我无法确定这是一个方法问题,还是JavaScript内置的Math.random()生成的数字不是完全均匀分布的 有人发现过类似的问题吗 在这里您可以找到ziggurat功能: 下面是Box Mull

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

我尝试过Box-Muller的方法和ziggurat,但生成的数字序列的平均值为0.0015或-0.0018-离零很远!!超过500000个随机生成的数字这是一个大问题。它应该接近于零,类似于0.000000000001

我无法确定这是一个方法问题,还是JavaScript内置的
Math.random()
生成的数字不是完全均匀分布的

有人发现过类似的问题吗

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

下面是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];
}

如果生成
n
独立的正态随机变量,则将是
sigma/sqrt(n)

在您的情况下,
n=500000
sigma=1
因此平均值的标准误差约为
1/707=0.0014
。如果平均值为0,则95%的置信区间约为该值的两倍或
(-0.0028,0.0028)
。你方样品的平均值在这个范围内

您获得
0.000000000001
1e-12
)的期望没有数学依据。要在该精度范围内,您需要生成大约
10^24
个样本。以每秒10000个样本的速度,仍然需要3年的时间来完成……这正是为什么尽可能避免通过模拟来计算事物的原因


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

如果您发布代码,将更容易为您提供帮助。最后一个问题:如果我必须生成一个对数正态分布的500000个数字序列,给定样本的平均值和方差,我是否必须调整一些参数以获得与原始样本完全相同的平均值?请查看如何调整为了计算对数正态分布给定参数的均值和方差:“每秒10000个样本,仍然需要3个四分之一年的时间”——我使用Redis,在MacBook Pro上用了6分钟。你生成了10^24个正态随机变量?我想你指的是500000;另外,Redis将比Javascript快得多。