Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seeding default_random_engine?

I'm using visual studio 2010 which doesn't support <chrono>, so I have to seed default_random_engine. Thus, I've decided to seed it with rand as following

srand((unsigned int)time(NULL));
std::default_random_engine engine(rand());
std::normal_distribution<double> randn(0.0, 0.3);

instead of the following

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine engine(seed);
std::normal_distribution<double> randn(0.0, 0.3);

I'm getting what I'm expecting to acquire for both methods. My question is are there any considerations should I pay attention to by using rand()? (Note: I have no choice to use <chrono>

like image 553
CroCo Avatar asked Feb 28 '14 21:02

CroCo


2 Answers

I recommend grabbing a seed from std::random_device:

std::default_random_engine engine(std::random_device{}());

which should provide you with significantly more entropy than std::time.

like image 196
Casey Avatar answered Sep 30 '22 07:09

Casey


According to http://www.cplusplus.com/reference/random/random_device/, they recommend that you don't use std::random_device, as it isn't portable:

Notice that random devices may not always be available to produce random numbers (and in some systems, they may even never be available).

On a related page (http://www.cplusplus.com/reference/random/linear_congruential_engine/linear_congruential_engine/), they give the following as an example of creating a seed:

unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
like image 35
Mark Ingram Avatar answered Sep 30 '22 05:09

Mark Ingram