# get a normally distributed random number in typescript

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.ts```function generateRandomNormal(mean: number, stdDev: number): number {
let u1 = Math.random();
let u2 = Math.random();
let z = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
return mean + stdDev * z;
}
```221 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.ts```// generate 10 normally distributed random numbers with mean 0 and standard deviation 1
for (let i = 0; i < 10; i++) {
let num = generateRandomNormal(0, 1);
console.log(num);
}
```181 chars6 lines```

### related categories

gistlibby LogSnag