In Typescript, you can use the random
function from the Math
object to generate a random number between 0 and 1. To get a normally distributed random number, you can use the Box-Muller transform algorithm.
Here's an example function that generates a normally distributed random number:
index.ts221 chars7 lines
The function takes in two parameters: the mean and standard deviation of the normal distribution you want to generate. It first generates two uniformly distributed random numbers (u1
and u2
). It then applies the Box-Muller transform to these numbers to get a normally distributed number z
. Finally, it scales and shifts z
by the specified mean and standard deviation and returns the result.
You can use this function to generate normally distributed random numbers in your Typescript programs.
index.ts181 chars6 lines
gistlibby LogSnag