I want to verify that if the seed is same, the resulting pseudo random number is same. Using rand() is the simplest but I'm looking for safer way. I found that using device entropy with /dev/random of linux is very reliable but cannot find how to set exact same seed. I also tried cryptgenrandom of window but cannot find the way to set seed. Can I make manual seed with those random generator, which use device things? Or is there any other random generator library which I can set manual seed?
2 Answers
Yes, <random>
allows this, and it's a good idea to switch to it. C++'s <random>
library is far superior to <stdlib>
's rand()
function, and allows you to set the seed each time. You should be using <random>
for any production code you write.

- 4,989
- 4
- 17
- 37
Is there any random generator library except rand(), which developer can manually set the seed?
Yes.
There are many available random number generators (and most are seedable) and all of them are better than rand()
(which is pretty bad). Don't use srand()
/rand()
in production code.
Check out the <random>
header.
I'd recommend std::mt19937
for non-cryptographic purposes (make sure to seed it properly, not just with a single int
), and std::random_device
if you need raw entropy (be sure to check that your implementation actually implements it properly, the standard - mistakenly IMHO - allows it to fall back to a PRNG). Stay away from std::default_random_engine
, you don't know what it will give you.

- 555,201
- 31
- 458
- 770

- 30,449
- 3
- 47
- 70