Im having problems generating normally distributed random numbers (mu = 0 sigma = 1) using JavaScript.
Ive tried the Box-Muller and ziggurat methods, but the average of the generated series of numbers turns out to be 0.0015 or -0.0018 - very far from zero! Over 500,000 randomly generated numbers is a big problem. It should be close to zero, something like 0.000000000001.
I can’t understand if its a problem with the method, or if the built-in JavaScripts Math.random()generate not exactly evenly distributed numbers.
Has anyone found similar problems?
Here you can find the ziggurat function:
http://www.filosophy.org/post/35/normaldistributed_random_values_in_javascript_using_the_ziggurat_algorithm/
And below is the code for Box-Muller:
function rnd_bmt() {
var x = 0, y = 0, rds, c;
do {
x = Math.random()*2-1;
y = Math.random()*2-1;
rds = x*x + y*y;
}
while (rds === 0 || rds > 1)
c = Math.sqrt(-2*Math.log(rds)/rds);
return [x*c, y*c];
}
source
share