Random_device vs default_random_engine

#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);?

Question 2: Which is better?

+3
source share
2 answers

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::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.

There is another subtle difference. std::random_device::operator()will throw an exception if he does not receive a random number.

Question 2: Which is better?

. , , , , .

+3

random_device default_random_engine . random_device , , prng . random_device, ( ). , , , , , mersenne_twister_engine.

, default_random_engine - , , , . , rand linear_congruential_engine .

, "" . , . , , default_random_engine - , , , random_device .

+4

All Articles