c++ - Best way to seed mt19937_64 for Monte Carlo simulations -



c++ - Best way to seed mt19937_64 for Monte Carlo simulations -

i'm working on programme runs monte carlo simulation; specifically, i'm using metropolis algorithm. programme needs generate perchance billions of "random" numbers. know mersenne twister popular monte carlo simulation, create sure seeding generator in best way possible.

currently i'm computing 32-bit seed using next method:

mt19937_64 prng; //pseudo random number generator unsigned long seed; //store seed every run can follow same sequence unsigned char seed_count; //to help maintain seeds repeating because of temporal proximity unsigned long genseed() { homecoming ( static_cast<unsigned long>(time(null)) << 16 ) | ( (static_cast<unsigned long>(clock()) & 0xff) << 8 ) | ( (static_cast<unsigned long>(seed_count++) & 0xff) ); } //... seed = genseed(); prng.seed(seed);

i have feeling there much improve ways assure non-repeating new seeds, , i'm quite sure mt19937_64 can seeded more 32-bits. have suggestions?

let's recap (comments too), want generate different seeds independent sequences of random numbers in each of next occurrences:

the programme relaunched on same machine later, two threads launched on same machine @ same time, the programme launched on 2 different machines @ same time.

1 solved using time since epoch, 2 solved global atomic counter, 3 solved platform dependent id (see how obtain (almost) unique scheme identifier in cross platform way?)

now point best way combine them uint_fast64_t (the seed type of std::mt19937_64)? assume here not know priori range of each parameter or big, cannot play bit shifts getting unique seed in trivial way.

a std::seed_seq easy way go, homecoming type uint_least32_t not our best choice.

a 64 bits hasher much improve choice. stl offers std::hash under functional header, possibility concatenate 3 numbers above string , passing hasher. homecoming type size_t on 64 machines match our requirements.

collisions unlikely of course of study possible, if want sure not build statistics include sequence more once, can store seeds , discard duplicated runs.

a std::random_device used generate seeds (collisions may still happen, hard if more or less often), since implementation library dependent , may go downwards pseudo random generator, mandatory check entropy of device , avoid utilize zero-entropy device purpose break points above (especially point 3). unfortunately can find entropy when take programme specific machine , test installed library.

c++ c++11 random random-seed mersenne-twister

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -