Normal Distribution (Log-normal Distribution) Random Numbers

Normal Distribution Random Numbers

 To obtain normally distributed random numbers with mean \(\mu\) and standard deviation \(\sigma\), there is a method called the Box-Muller method. This method uses the fact that, when \( \alpha, \beta\) are uniform random numbers in the range [0,1], the following two values

follow a normal distribution with mean 0 and standard deviation 1. Although this requires generating two uniform random numbers in the range [0,1], the two values above are uncorrelated, so two independent normally distributed random numbers are obtained at once.
By multiplying the resulting values by \(\sigma\) and then adding \(\mu\), you can generate normally distributed random numbers with any desired mean and standard deviation.
 The C# code is shown below.

// Obtain normally distributed random numbers with mean mu and standard deviation sigma, using the Box-Muller method.
public static double NormalDistribution(double mu, double sigma)
{
    if (Flag)
    {
        Alpha = Rn.NextDouble();
        Beta = Rn.NextDouble() * Math.PI * 2;
        BoxMuller1 = Math.Sqrt(-2 * Math.Log(Alpha));
        BoxMuller2 = Math.Sin(Beta);
    }
    else
        BoxMuller2 = Math.Cos(Beta);
    Flag = !Flag;
    return sigma * (BoxMuller1 * BoxMuller2) + mu;
}
static Random Rn = new Random(System.Environment.TickCount);
static double Alpha, Beta, BoxMuller1, BoxMuller2;
static bool Flag = false;

Log-Normal Distribution Function

 Next, let us discuss log-normal distribution random numbers. A log-normal distribution is a distribution that follows a normal distribution when the horizontal axis (\(X\) axis) is transformed to a logarithmic scale.
 First, let the mean and standard deviation of a regular normal distribution function \(f(x)\) be \(m\) and \(s\), respectively. Furthermore, let the mean of the logarithm-transformed horizontal axis (i.e., the mean of \(\ln(x)\)) be \(\mu\), and the standard deviation (the standard deviation of \(\ln(x)\)) be \(\sigma\). The following relationships hold between \(m, s, \mu, \sigma\):

Rearranging with \(\mu\) and \(\sigma\) on the left-hand side:

Using this property, the code for generating log-normal distribution random numbers can be written as follows.

// Obtain log-normally distributed random numbers with mean mu and standard deviation sigma.
// Convert the mean and standard deviation, then call the NormalDistribution function based on the Box-Muller method.
 public static double LogNormalDistribution(double mu, double sigma)
 {
     if (mu <= 0)
         return double.NaN;
     else
     {
         var sigma2= sigma * sigma;
         var val = NormalDistribution(Math.Log(mu) - Math.Log(mu + sigma2) / 2.0, Math.Sqrt(Math.Log(1 + sigma2 / mu / mu)));
         return Math.Exp(val);
     }
 }
contents