Generate a random number between 0 and 1 with Gaussian distributions

I want to write a method in C # to generate a random number with a Gaussian distribution in the range [0:1] (and in advance in [0-x]). I found this code but not working correctly

 Random rand = new Random(); //reuse this if you are generating many double u1 = rand.NextDouble(); //these are uniform(0,1) random doubles double u2 = rand.NextDouble(); double randStdNormal = Math.Abs( Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2)); 
+7
source share
3 answers

I wrote a blog post on how to create random numbers with any given distribution:

http://ericlippert.com/2012/02/21/generating-random-non-uniform-data/

To summarize, you need an algorithm:

  • Develop the desired probability distribution function so that the area under the part of the curve is equal to the probability that the value will be randomly generated in this range.
  • Integrate the probability distribution to determine the cumulative distribution .
  • Invert cumulative distribution to obtain a quantility function .
  • Transform your evenly distributed (0,1) random data by running them through the quantile function.

Of course, if you already know the quantile function for the desired distribution, you do not need to take steps one or three.

+2
source

You say you want a generator for normally distributed (Gaussian) random numbers from 0 to 1.

First of all, the normal distribution is not limited ... the function that you show in your example generates normally distributed random numbers with an average of 0.0 and a standard deviation of 1.0

You can generate normally distributed random values โ€‹โ€‹of any mean and standard deviation by multiplying the value you get from this function by the desired standard deviation, and then add the desired mean value ...

The code is in order as it is - the problem is a misunderstanding of the Gaussian (normal) distribution, which has a range from -inf to + inf ...

at about 2/3 of the time when the value you get will be between the standard deviation +/- 1 ... in about 95% of cases the value will be between + / 1 3 times the standard deviation ...

+1
source

What about a sigmoid or -sigmoid that computes 1 / (1 + e ^ (-x)), where x is a random number from a Gaussian distribution?

-one
source

All Articles