Eta*_*Eta 12
我做了一些关于高斯近似的研究.我在精度/性能方面的最佳表现是:
var gaussrand =(Math.random()+ Math.random()+ Math.random()+ Math.random()+ Math.random()+ Math.random() - 3);
这个技巧可能看起来很难看,但它会给你一个平均值,sigma²= 1/2,如预期的那样.
我希望这将有所帮助.
这是代码,使用维基百科的近似值,如 Peter Mortensen 的响应中所述。原始信用归Abramowitz 和 Stegun。
function erf(x) {
var z;
const ERF_A = 0.147;
var the_sign_of_x;
if(0==x) {
the_sign_of_x = 0;
return 0;
} else if(x>0){
the_sign_of_x = 1;
} else {
the_sign_of_x = -1;
}
var one_plus_axsqrd = 1 + ERF_A * x * x;
var four_ovr_pi_etc = 4/Math.PI + ERF_A * x * x;
var ratio = four_ovr_pi_etc / one_plus_axsqrd;
ratio *= x * -x;
var expofun = Math.exp(ratio);
var radical = Math.sqrt(1-expofun);
z = radical * the_sign_of_x;
return z;
}
Run Code Online (Sandbox Code Playgroud)