I am trying to find an efficient way to implement a uniform distribution (0,1). Since I have to generate a very large number of samples, I chose mt19937 as the engine. I am using the version from the boost library. My question is: what is the difference between using the engine of the engine itself versus using uniform_real_distribution?
Option number 1
std::random_device rd; boost::mt19937 gen(rd()); boost::random::uniform_real_distribution<double> urand(0, 1); for ( int i = 0; i < 1E8; i++ ) { u = urand(gen); }
Option number 2
std::random_device rd; boost::mt19937 gen(rd()); for ( int i = 0; i < 1E8; i++ ) { u = (double) gen()/gen.max(); }
Of my tests, Option # 2 is significantly better than Option # 1 in terms of runtime. Is there any reason why I should choose option # 1 over Option # 2?
source share