DEV Community

Cover image for Random normal distribution
Grzegorz Kućmierz
Grzegorz Kućmierz

Posted on

Random normal distribution

JavaScript by default don't have randomness with normal distribution.

We can use for that pretty simple Box-Muller implementation that I showed below.

// Box-Muller implementation:
const randNormal = (mean, sigma) => {
  const y1 = Math.random();
  const y2 = Math.random();
  return (mean + sigma *
    Math.cos(2 * Math.PI * y2) *
    Math.sqrt(-2 * Math.log(y1))
  );
};
Enter fullscreen mode Exit fullscreen mode

It can be useful to solve this codewars kata.

You can see example use of this code here.

Top comments (0)