Offset in randomization of normally distributed numbers (javascript)

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;

    // 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];
}
+5
source share
1 answer

If you create independent ordinary random variables n, the standard deviation of the mean will be sigma / sqrt(n).

In your case n = 500000and sigma = 1, therefore, the standard error of the mean is approximately equal 1 / 707 = 0.0014. The 95% confidence interval set to 0 will be approximately twice that or (-0.0028, 0.0028). Your picker is within this range.

0.000000000001 (1e-12) . , 10^24 . 10000 , - 3 ... , .

, , , :)

+5

All Articles