From here, you can see std::random_device
does not always guarantee to be non-deterministic:
std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.
On Linux, it by default uses /dev/urandom
or RDRND
CPU instruction according to here:
The implementations in libc++ and libstdc++ expect token to be the name of a character device that produces random numbers when read from, with the default value "/dev/urandom", although where the CPU instruction RDRND is available, libstdc++ uses that as the default.
which won't block. You can switch to a even secured device /dev/random
using approach here, but that device will block if not having enough entropy.
On Windows, I'm not sure about such device therefore it might fall back to some PRNG which requires some kind of seed.
To solve the problem cross-platformly, as @Binara mentioned, you can use std::rand
from <cstdlib>
. This function will not block, and you can use std::srand(somethingLikeCurrentTime)
to make it some-what non-deterministic.
As @user1118321 mentioned, if you want to use a more secured PRNG, you might consider std::mersenne_twister_engine
and use std::random_device
to generate a seed of it. This approach is suggested here.