Also, why not just:
std::mt19937 e{std::random_device{}()};
It may be good if you only do it once, but if you do it many times, it is better to keep track of your std::random_device , rather than creating / destroying it unnecessarily.
It may be useful to look at the source code of libC ++ for the implementation of std::random_device , which is pretty simple. This is just a thin wrapper over std::fopen("/dev/urandom") . Therefore, every time you create std::random_device , you get a different file system descriptor and pay all the associated costs.
On Windows, as I understand it, std::random_device is some std::random_device microsoft crypto API call, so you will initialize and destroy some cryptographic program library interface every time you do it.
It depends on your application, but for general purposes I would not think about this overhead, as always, negligible. Sometimes it is, and then itβs great.
I think this is related to your first question:
Instead, this is what I found on most examples / sites / articles:
std::random_device rd; std::mt19937 e{rd()}; // or std::default_random_engine e{rd()}; std::uniform_int_distribution<int> dist{1, 5};
At least I think about it:
std::mt19937 is a very simple and reliable random generator. The implementation is obliged by the standard, and, at least in boost, it everywhere used the same code obtained from the original article mt19937 . This code is very stable and cross-platform. You can be sure that initialization, a request from it, etc. You are going to compile similar code on any platform on which you compile it, and that you will get the same performance.
std::random_device contrast, is pretty opaque. You really do not know exactly what it is, what it is going to do, or how effective it will be. You donβt even know if it can really be received - it can throw an exception when you try to create it. You know that this does not require seed. Usually you should not extract tons and tons of data from it, just use them to generate seeds. Sometimes it acts as a good interface for cryptographic APIs, but in fact it is not required, and, unfortunately, sometimes it is not. It can match /dev/random on unix, it can match /dev/urandom/ . It may correspond to some cryptographic API MSVC (visual studio), or it may just be a fixed constant (mingw). If you cross-compile a phone, who knows what it will do. (And even if you get /dev/random , you still have a problem that performance can be consistent - it might seem like it works fine until the entropy pool runs out, and then it runs slowly, like a dog.)
As I think of it, std::random_device should look like an improved version of the sowing with time(NULL) - a low bar, because time(NULL) is a pretty crappy seed, everything considered. I usually use it, where I would use time(NULL) to generate the seed for the day. I really don't find all this useful outside of this.
Chris beck
source share