jeudi 3 septembre 2020

How to initialize std::mt19937 when std::random_device entropy is unknown? (VS C++/Windows)

I am attempting to build a simple random number generator, but I wanted to make sure random_device was working properly. I started with the following code:

#include <random>
#include <chrono>

class Generator {
public:
    Generator()
        :
        m_DeviceSeed(rd()),
        m_TimeSeed(std::chrono::high_resolution_clock::now().time_since_epoch().count()),
        rng(m_DeviceSeed)
    {
        if (rd.entropy() == 0.0) {
            rng.seed((unsigned)m_TimeSeed);
        }
    }
private:
    //Vars
    unsigned int m_DeviceSeed;
    unsigned long long m_TimeSeed;
    std::random_device rd;
    std::mt19937 rng;
};

I had seen "std::chrono::high_resolution_clock::now().time_since_epoch().count()" recommended as an alternative to random_device, and I figured checking entropy would allow me to use it as a fallback; however, this is written in Visual Studio, and apparently that means entropy always shows 32, regardless of if it is true or not.

So, my question is this: what is the most robust way to seed std::mt19937 without a means of testing entropy? Is chrono better, or random_device? Or some combination, or other option entirely?

Based off of this: The implementation of random_device in VS2010?

It seems random_device is a safe pick for seeding or generating seed_sequences in most situations, but I want to be sure.




Aucun commentaire:

Enregistrer un commentaire