How does std::random_device generate "true" random numbers

I just read on this website that the function std::random_device() is not like other 'generators' that generate pseudo-random numbers.

It generates true stochastic (non-deterministic) random numbers based on the distribution, if it is present.

I'm just curious as to how such a function might be implemented internally. Also, why isn't it possible for all environments?

Anyone has any idea?

Thanks
Last edited on
It will generally need to be connected to a hardware device or access true random numbers from an online service. I've heard of such devices measuring radioactive decay to retrieve random numbers for example. If a device is unavailable random_device will fallback to a pseudo random generator.
I'm just curious as to how such a function might be implemented internally
For modern CPU this can be used for source of stochastic numbers:
http://stackoverflow.com/a/18004959/3410396
why isn't it possible for all environments?
Some enviroments does not have reliable source of non-deterministic numbers.
Thanks for your replies @LachlanEaston and @MiiNiPaa.

So does the code in the function implementation look something like:

1
2
3
4
if (CPU is compatible)
  - generate true random number
else
  - fallback to pseudo random generator
Close to that, except there's also a "fallback to OS support" intermediate stage

See, for example, the GCC implementation, where the fallback from the Intel CPU command RDRND is to /dev/urandom, although the caller of random_device can opt for /dev/random: https://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/src/c%2B%2B11/random.cc?view=markup#l77 -- on Linux this "gathers environmental noise from device drivers and other sources"
Last edited on
Aah, ok..

So the function random_device first tries to 'delegate' the random number generation to CPU. If it fails, then to the OS, and finally to the psuedo random generator.

Thanks for the link!
Topic archived. No new replies allowed.