Search code examples
c++assemblyc++11randomrdrand

True random numbers with C++11 and RDRAND


I have seen that Intel seems to have included a new assembly function to get real random numbers obtained from hardware. The name of the instruction is RdRand, but only a small amount of details seem accessible on it on Internet: http://en.wikipedia.org/wiki/RdRand

My questions concerning this new instruction and its use in C++11 are the following:

  1. Are the random numbers generated with RdRand really random? (each bit generated from uncorrelated white noise or quantum processes? )

  2. Is it a special feature of Ivy Bridge processors and will Intel continue to implement this function in the next generation of cpu?

  3. How to use it through C++11? Maybe with std::random_device but do compilers already call RdRand if the instruction is available?

  4. How to check whether RdRand is really called when I compile a program?


Solution

    1. That certainly depends on your view of the determinism of the universe, so is more a philosophical question, but many people consider it being random.
    2. Only intel will know, but since there was demand to add it, its likely there will be demand to keep it
    3. std::random_device is not required to be hardware driven, and even if it is, it is not required to use rdrand. You can ask its double entropy() const noexcept member function whether it is hardware driven or not. Using rdrand for that is a QoI issue, but I would expect every sane implementation that has it available to do so (I have seen e.g. gcc doing it). If unsure, you can always check assembly, but also other means of hardware randomness should be good enough (there is other dedicated hardware available).
    4. See above, if you are interested in whether its only hardware, use entropy, if interested in rdrand, scan the generated machine code.