简体   繁体   中英

C++ normal_distribution function for simulation application

I was wondering what kind of random number generator does the normal_distribution function use ?

Does it fit for scientific simulation application ?

Regards

std::normal_distribution doesn't do any random number generation. It is a random number distribution . Random number distributions only map values returned by a random number engine to some kind of distribution. They don't do any generation themselves. So it is the random number engine that you care about.

One of the random number engines provided by the standard, the std::mersenne_twister_engine is a very high quality random number engine. You can use it to generate random numbers with a normal distribution like so:

std::random_device rd;
std::mt19937 gen(rd()); // Create and seed the generator
std::normal_distribution<> d(mean, deviation); // Create distribution
std::cout << d(gen) << std::endl; // Generate random numbers according to distribution

Note that std::mt19937 is a typedef of std::mersenne_twister_engine .

The whole point of the <random> standard library is to separate distributions from random number generators. You supply a random number generator that generates uniform integers, and the distribution takes care of transforming that random, uniform integer sequence into a sample of the desired distribution.

Fortunately, the <random> library also contains a collection of random number generators. The Mersenne Twister ( std::mt19937 ) in particular is a relatively good (ie fast and statistically high quality) one.

(You also need to provide a seed for the generator.)

I know the post is old, however, I hope my answer is beneficial. I use normal_distribution to generate a Gaussian noise for a sensor. This is beneficial for simulating sensors. For example, let's say you have a sensor that gives you the position of a robot in 2D. Every time you move the robot, the sensor gives you some readings of the position of your robot. In OpenGL, you can simulate this example. For example, you can track the position of the mouse and add some Gaussian noise to the real position of the mouse. In this case, you have a sensor that track the position of the mouse, however it have uncertainty due to the noise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM