2009-12-15 14 views

回答

4

以下是使用approximation from wikipedia的代碼,如Peter Mortensen的回覆中所述 原始積分爲Abramowitz and 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; 
     } 
12

我對高斯近似做了一些研究。 (Math.random())+ Math.random()+ Math.random()+ Math.random()+ Math.random()+數學。數學公式隨機() - 3);

這個技巧可能看起來很醜,但它會給你一個平均值和sigma²= 1/2的預期。

我希望這會有所幫助。

+1

這是有效的,因爲中心極限定理 – sauerburger 2015-10-15 11:11:17