C ++随机引擎作为全局变量

时间:2017-04-21 09:08:21

标签: random global-variables normal-distribution

我将使用其随机性的分布生成一些随机数据。 正如其他地方所建议的那样,一次性初始化随机engine是一种很好的做法。这就是我所做的,将其定义为全局变量以及我选择的几个发行版:

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()
{...
...}

然而,当我呼叫任何distribution(带有关联的engine)时,即

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

}

通过任何函数,编译器说

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

使用全局变量时,我是否遗漏了一些东西?

0 个答案:

没有答案