One way to sample from a normal distribution in JavaScript is to use the random
method of the Math
object in combination with the Box–Muller transform. The Box–Muller transform is a method for generating a pair of independent standard normally distributed random variables (i.e., with mean 0 and standard deviation 1) from two uniform random variables.
Here's an example function that uses the Box–Muller transform to generate a single random number from a normal distribution with mean mean
and standard deviation stdDev
:
index.tsx199 chars7 lines
To generate multiple random numbers from the same normal distribution, you can simply call this function multiple times.
gistlibby LogSnag