#include <vector> #include <random> using namespace std; int main() { vector<int> coll{1, 2, 3, 4}; shuffle(coll.begin(), coll.end(), random_device{}); default_random_engine dre{random_device{}()}; shuffle(coll.begin(), coll.end(), dre); }
Question 1: What is the difference between
shuffle(coll.begin(), coll.end(), random_device{});
and
shuffle(coll.begin(), coll.end(), dre);?
shuffle(coll.begin(), coll.end(), dre);
Question 2: Which is better?
Question 1: What is the difference between ...
std::random_deviceconceptually produces true random numbers. Some implementations will stop if you run out of source of system entropy so that this version also cannot work.
std::random_device
std::default_random_engine- pseudo-random engine. After sowing with a random number, it would be extremely difficult (but not impossible) to predict the next number.
std::default_random_engine
There is another subtle difference. std::random_device::operator()will throw an exception if he does not receive a random number.
std::random_device::operator()
. , , , , .
random_device default_random_engine . random_device , , prng . random_device, ( ). , , , , , mersenne_twister_engine.
random_device
default_random_engine
mersenne_twister_engine
, default_random_engine - , , , . , rand linear_congruential_engine .
rand
linear_congruential_engine
, "" . , . , , default_random_engine - , , , random_device .