JavaScript を使用して正規分布乱数 (mu=0 sigma=1) を生成する際に問題があります。
Box-Muller の方法とジッグラトを試してみましたが、生成された一連の数値の平均は 0.0015 または -0.0018 になり、ゼロにはほど遠いです!! ランダムに生成された 500,000 を超える数字は大きな問題です。0.000000000001 のように、0 に近いはずです。
それがメソッドの問題なのか、それとも JavaScript のビルトインがMath.random()
正確に均一に分布していない数値を生成するのか、私にはわかりません。
誰かが同様の問題を見つけましたか?
ここでは、ジグラット関数を見つけることができます。
以下は、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];
}