everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
21 lines (20 loc) • 737 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.randomGaussian = void 0;
/**
* Returns a random number from a normal (Gaussian) distribution.
* @author @dailker
* @param {number} [mean=0] - The mean of the distribution.
* @param {number} [std=1] - The standard deviation of the distribution.
* @returns {number} A random number from the normal distribution.
*/
function randomGaussian(mean = 0, std = 1) {
// Box-Muller transform
let u = 0, v = 0;
while (u === 0)
u = Math.random();
while (v === 0)
v = Math.random();
return mean + std * Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
}
exports.randomGaussian = randomGaussian;