Home > Backend Development > C++ > How Can I Generate Random Normal Deviates in C/C Using the Box-Muller Transform?

How Can I Generate Random Normal Deviates in C/C Using the Box-Muller Transform?

Patricia Arquette
Release: 2024-11-27 18:48:15
Original
773 people have browsed it

How Can I Generate Random Normal Deviates in C/C   Using the Box-Muller Transform?

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)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template