Guassian Random Number Generator

Ok, we all know that everything on the Internet is true. So, I found a post at http://www.protonfish.com/random.shtml on how to generate random numbers with a Gaussian Distribution in JavaScript. Long story short, I've been burned by random number generators before, so the first thing that anyone should do is make sure that it actually does work correctly.

Note that this isn't a fully mathematical proof, just testing to make sure that the random number generator is creating the correct distribution. There could still be underlying patterns in the generator which makes this an in-appropriate generator.


function guassian_seed() {
  return (Math.random() * 2 - 1) + (Math.random() * 2 - 1) + (Math.random() * 2 - 1);
}

function rnd(mean, stdDev) {
  return Math.round(guassian_seed() * stdDev + mean);
}

Published on 4 February 2015

Brian Hoover is a full stack software engineer with many years of experience. He's also a facilitator at the University of Phoenix