Generating Random Normal Deviates in C/C
Generating random numbers that follow a normal distribution is a common task in various applications. In C/C , there are several approaches to achieve this without relying on external libraries like Boost.
One widely employed method is the Box-Muller transform, which transforms two uniformly distributed random numbers into a pair of normally distributed random numbers. The mathematical formula is as follows:
x = sqrt(-2 * ln(u1)) * cos(2 * M_PI * u2) y = sqrt(-2 * ln(u1)) * sin(2 * M_PI * u2)
where u1 and u2 are uniformly distributed random numbers in the range [0, 1]. To generate a single random normal deviate, generate a pair using the Box-Muller transform and return one of them, storing the other for subsequent use.
The above is the detailed content of How Can I Generate Random Normal Deviates in C/C Using the Box-Muller Transform?. For more information, please follow other related articles on the PHP Chinese website!