vendredi 21 avril 2017

C++ Random engine as global variable

I'm going to generate some random data, using a distribution for its randomness. As suggested somewhere else, it is a good practice to initialize once and for all a random engine. Here is what I did, defining it as a global variable along with a couple of distributions I choose:

default_random_engine engine; /*Random engine*/
engine.seed(time(0));
uniform_real_distribution<double> uniform(0,2.0*M_PI);
normal_distribution<double> normal(0,1);

int main()
{...
...}

However, when I call any distribution (with the associated engine), i.e.

void myFun()
{
 double rnd=normal(engine);

}

through any function, compiler says

error: ‘engine’ was not declared in this scope
error: ‘normal’ was not declared in this scope

Is there something I'm missing with the usage of global variables?

Aucun commentaire:

Enregistrer un commentaire