To generate a normally distributed random number in C#, we can make use of the Box-Muller transform. Here's an example implementation:
main.cs325 chars9 lines
This method takes three parameters: a Random
instance for generating the uniform random numbers, the mean of the distribution, and the standard deviation of the distribution. It then generates two uniform random numbers, uses them to compute a standard normal random variable using the Box-Muller transform, and scales the result to the desired mean and standard deviation.
We can use this method like this:
main.cs119 chars3 lines
This will generate a normally distributed random number with a mean of 0 and a standard deviation of 1.
gistlibby LogSnag