I was wondering if there is a way to make use of the new hardware based true number generator found in intel's sandy bridge CPU? I read that intel's MKL (Math Kernel Library) exposes this functionality, but this requires the MKL suite and an intel complier, ending up pretty expensive.
Is there another way to employ the hardware random number generator in my C++ code? For example a nice, header only library?
A TRNG is a function or device based on an unpredictable physical phenomenon, called an entropy source, that is designed to generate non-deterministic data (e.g., a succession of numbers) to seed security algorithms.
A hardware random number generator typically consists of a transducer to convert some aspect of the physical phenomena to an electrical signal, an amplifier and other electronic circuitry to increase the amplitude of the random fluctuations to a measurable level, and some type of analog-to-digital converter to convert ...
There are two main methods that a computer generates a random number: true random number generators (TRNGs) and pseudo-random number generators (PRNGs). The former uses some phenomenon outside the computer for its number generation, whereas the latter relies on pre-set algorithms to emulate randomness².
There are generally two kinds of random number generators: non-deterministic random number generators, sometimes called "true random number generators" (TRNG), and deterministic random number generators, also called pseudorandom number generators (PRNG).
Intel has posted a manual, library, and code examples for the rdrand
instruction at http://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide.
From the Readme:
"Because the many of compiler toolchains do not support this new instruction, this library was created to facilitate easy access to it. The idea is simple: link to a built static library and enjoy the new feature!"
There are examples of all the library calls in main.c
.
I was able to compile the static library and test program in gcc on Mac OS X. The documentation states that it is also compatible with Linux and Windows.
Be aware that rdrand
is actually a 128-bit pseudo-random number generator with hardware-generated entropy. (The upcoming Broadwell architecture will provide an rdseed
instruction to access the true random number generator.) The details of the difference and its implications can be found under the "Long Answer" heading at http://software.intel.com/en-us/blogs/2012/11/17/the-difference-between-rdrand-and-rdseed.
Here is the example code:
#include <immintrin.h>
#include <cstdint>
...
uint64_t val;
if(!_rdseed64_step(&val)) {
printf("Error generating hardware random value\n");
}
// Now val contains 64-bit pseudo-random number
uint64_t val;
if(!_rdrand64_step(&val)) {
printf("Error generating hardware random value\n");
}
// Now val contains 64-bit true random number
Reference: Intel Intrinsics Guide
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With