c++ - Using random number generator: multiple instances or singleton approach? -


i created class can use random number generator works better standard rand(). below i've included .cpp file class contains class variables std::mt19937 gen , std::uniform_real_distribution distr.

my question whether it's necessary create multiple instances of number generator. instance if have classes , b , each class needs sample random numbers both within range [0,1] better if , b each had own instance of uniformnumbergenerator or should take singleton approach , use 1 instance both classes?

uniformnumbergenerator::uniformnumbergenerator(double min, double max) {     gen = creategenerator();     distr = std::uniform_real_distribution<double>(min, max); }  std::mt19937 uniformnumbergenerator::creategenerator() {     std::random_device rd;     std::mt19937 result(rd());     return result; }  //take sample double uniformnumbergenerator::operator()() {     return distr(gen); } 

one legitimate reason have multiple instances of pseudo-random number generator (prng) increase precision of simulation estimates of difference between 2 systems using correlation induction strategies. simplest of these "common random numbers" (crn)*. intuitively, if have 2 different arrangements of bank or grocery store , want determine configuration works best, make sense compare them same set of customers , transactions. crn generating same sequence of prns both scenarios, have careful keep prng streams in sync. can real challenge if 2 systems have different quantities of permanent entities such servers. in case, 1 prng per server appropriate, , 1 prng generate characteristics each arrival stream well. prngs independently seeded within run (so servers appear independent of each other), , identically seeded between runs yield run-level synchronization.

bottom line -- unless plan use correlation induction strategies variance reduction , you're sure know you're doing, should use single prng. mt19937 produces sequentially uncorrelated k-tuples large values of k, on 600 if recall correctly. if goal generate independent samples, that's mt19937 , other decent generators designed mimic via single instance of generator.

* - there's wikipedia article well, it's written poorly don't recommend using resource.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -