python - Gaussian distribution with mean and sigma in C++11 -
i trying gaussian distribution mean , sigma in c++11. have been successful @ converting python c++ have doubt way initializing random generator. need call random_device() , mt19937() inside call distribution or can call them once statically , re-use time? cost of leaving code is?
# python # random.gauss(mu, sigma) # gaussian distribution. mu mean, , sigma standard deviation. import random result = random.gauss(mu, sigma) // c++11 #include <random> std::random_device rd; std::mt19937 e2(rd()); float res = std::normal_distribution<float>(m, s)(e2);
there 2 parts of algorithm:
- uniform random number generator,
- and convert uniform random number random number according gaussian distribution.
in case, e2
uniform random number generator given seed rd
, std::normal_distribution<float>(m, s)
generates object 2nd part of algorithm.
the best way is:
// call first time (initialization) std::random_device rd; std::mt19937 e2(rd()); std::normal_distribution<float> dist(m, s); // bind distribution generator , uniform generator auto gen_gaussian = std::bind(dist, e2); // call when need generate random number float gaussian = gen_gaussian();
if don't care uniform random number generator use, can use std::default_random_engine
instead of std:mt19937
.
Comments
Post a Comment