Generating Normally Distributed Random Numbers in JavaScript

JavaScript's pseudo-random number generator Math.random() outputs what you want a pseudo-random number generator to output: a random number uniformly distributed between 0 and 1. Great, but say you are in a situation where you don't want every number to be equally likely, you want them evenly distributed about a mean. You want a normally distributed random number. To get this you can use the Box-Muller transformation on two uniformly distributed random numbers. Problem solved!

Click to launch demo
normal preview

In this demonstration I use the Box-Muller transformation on numbers generated by JavaScript's Math.random() and use the Kolmogorov-Smirnov test to compare the result against a normal cdf table generated by Mathematica to see if they actually come out normally distributed.

As for results, generating anywhere between 50 and 1000 almost always passes the test with α=0.005. More than 1000 and it almost always fails. This could be for a number of reasons. First, in a word(s), rounding error. Second, I am using sqrt(1.63)/N to estimate the critical value for the test statistic because the tables for the Kolmogorov distribution I have only go up to 50. I suspect this estimate gets to be pretty far off by N=1000. Just eyeballing the resulting distribution after generating a large number (hundreds of thousands) it is consistently 3% heavy around the middle. Unacceptable? Depends, what do you need a browser to generate a normally distributed random number with more than 97% confidence for?

Tags:

Comments

Member since:
16 April 2011
Last activity:
38 weeks 23 hours

Nice post. I learn something tougher on completely different blogs everyday. It will all the time be stimulating to read content material from different writers and follow a little bit something from their store. I’d want to use some with the content material on my blog whether you don’t mind. Natually I’ll offer you a link in your net blog. Thanks for sharing.
Vicodin phooey Kamagra Bream Phentermine disinherit Meridia unstructured

Member since:
20 March 2011
Last activity:
44 weeks 6 days

You made some decent points there. I appeared on the web for the difficulty and located most individuals will associate with with your website.
Viagra Assemblage

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.