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 her to launch demo

xnormal.gif.pagespeed.ic.5sm4MdfRwQ

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.

 

Recent Posts

Meta

nwellcome Written by: