【发布时间】:2013-03-22 17:58:17
【问题描述】:
我正在做一些随机数生成并获得可疑行为。这是我的代码:
// initialized earlier... in the constructor of a class
boost::mt19937 *rng = new boost::mt19937();
rng->seed(time(NULL));
// actual use here.
for (int i = 0; i < 10; ++i)
{
test();
}
void test()
{
boost::normal_distribution<> distribution(10, 10);
boost::variate_generator< boost::mt19937, boost::normal_distribution<> > resampler(*rng, distribution);
const double sample = (resampler)(); // always the same value.
}
我是否在 boost 中滥用了随机抽样?我做错了什么以使其始终具有相同的价值。我在构造函数中初始化了随机数生成器,所以它应该总是吐出一个不同的值(没有重新初始化)
【问题讨论】:
-
你总是得到什么价值?
-
@jeremy 该值取决于程序的运行,因为 RNG 是按时间播种的。但是,在该程序的执行中每次运行 test() 总是相同的。
-
time(NULL) 返回什么,种子期望什么?也许那个种子正在截断种子值