Deterministic random number streams in C ++ STL

I want to specify a number, and then get a set of random numbers. However, I want these numbers to be the same regardless of which computer I run it on (provided that I put the same seed).

Basically, my question is: in C ++, if I use rand(), but put srand()with a custom seed, and not with the current time, can I generate the same random number stream on any computer?

+5
source share
7 answers

srand()and rand()are not part of the STL. They are actually part of the C runtime. Yes, they will produce the same results as long as it is the same implementation srand()/rand().

Depending on your needs, you may want to use Boost.Random . It provides several high quality random number generators.

+5
source

There are dozens of PRNGs as libraries . Choose one. I tend to use the Mersenne Twister .

, , rand(). , .

MT - , , -, . MT PRNG!

+7

, rand() , .

, rand() - , , .

+4

, ANSI C , rand() 0 RAND_MAX, 32767 (). , .

PRNG. Mersenne Twister ( ) , , C99- PRNG. Boost.Random ; ++, Boost ( "" (.. ) ). , , , , " " " x86/PPC/ARM/SPARC/Alpha/etc., GCC", .

+1
source

I believe that if you supply srand with the same seed, you will get the same results. This is pretty much the definition of a seed in terms of pseudo random number generators.

0
source

Yes. For a given seed (initial value), the sequence of numbers returned by the rand () function will always be the same.

0
source

All Articles