Last active
August 29, 2015 14:01
-
-
Save libo1106/afa34223c4b4820297b1 to your computer and use it in GitHub Desktop.
正态随机数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var randomGaussian = function(a,b){ | |
var c0 = 2.515517; | |
var c1 = 0.802853; | |
var c2 = 0.010328; | |
var d1 = 1.432788; | |
var d2 = 0.189269; | |
var d3 = 0.001308; | |
var f = 0; | |
var w = 0; | |
var r = Math.random(); | |
if(r <= 0.5){ | |
w = r; | |
}else{ | |
w = 1 - r; | |
} | |
if((r - 0.5) > 0){ | |
f = 1; | |
}else if((r - 0.5) < 0){ | |
f = -1; | |
} | |
var y = Math.sqrt((-2) * Math.log(w)); | |
var x = f * (y - (c0 + c1 * y + c2 * y * y) / (1 + d1 * y + d2 * y * y + d3 * y * y * y)); | |
var z = a + x * Math.sqrt(b); | |
return z; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment