I would like to be able to use the hardware random number generator, when available and regardless of intel or amd running the code, with C++ random library:
void randomDeviceBernouilli(double bernoulliParameter, uint64_t trialCount) {
std::random_device randomDevice;
std::cout << "operator():" << randomDevice() << std::endl;
std::cout << "default random_device characteristics:" << std::endl;
std::cout << "minimum: " << randomDevice.min() << std::endl;
std::cout << "maximum: " << randomDevice.max() << std::endl;
std::cout << "entropy: " << randomDevice.entropy() << std::endl;
std::cout << "a random number: " << randomDevice() << std::endl;
std::bernoulli_distribution bernoulliDist(bernoulliParameter);
uint64_t trueCount{ 0ull };
for (uint64_t i = 0; i < trialCount; i++)
if (bernoulliDist(randomDevice))
trueCount++;
std::cout << "Success " << trueCount << " out of " << trialCount << std::endl;
const double successRate = (double)trueCount / (double)trialCount;
std::cout << "Empirical: " << std::fixed << std::setprecision(8) << std::setw(10) << successRate << " Theoretical: " << bernoulliParameter << std::endl;
}
According to this article, entropy()
should have returned 0 on a cpu without RDRAND, such as the i7-2670qm ivy bridge (on which I've tested-RDRAND first appeared in its successor, sandy bridge), but it is always 32 in Visual Studio as documented here. It has been suggested that the absence of a random device may cause operator()
to throw an exception, but that does not happen either.
One can use the intrinsic int _rdrand32_step (unsigned int* val)
for example but that only draws from a uniform distribution, I need to be able to make use of the distributions in the C++'s random library.
Also, the code should make use of the hardware generator on an AMD cpu.
What is the proper way to use the hardware random number generator(RDRAND) with the C++'s random
library in Visual Studio(2017, 2019)?
Aucun commentaire:
Enregistrer un commentaire