Generate Gaussian random numbers
JavaScript, Math, Random · Apr 3, 2023

Generates Gaussian (normally distributed) random numbers.
- Use the Box-Muller transform to generate random numbers with a Gaussian distribution.
const randomGauss = () => {
const theta = 2 * Math.PI * Math.random();
const rho = Math.sqrt(-2 * Math.log(1 - Math.random()));
return (rho * Math.cos(theta)) / 10.0 + 0.5;
};
randomGauss(); // 0.5
Written by Angelos Chalaris
I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.
If you want to keep in touch, follow me on GitHub.