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?
Advertisement
Answer
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.