【发布时间】:2016-06-22 15:59:31
【问题描述】:
使用boost::random 我正在尝试从不同范围内对一组均匀分布的整数进行采样,但使用相同的底层随机数生成器。对于每个范围,我定义了一个返回随机数的不同函数。 但是,似乎每个函数都返回相同的数字。
说明该方法的示例:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/random.hpp>
struct RandomNumberGenerator {
boost::mt19937 generator;
RandomNumberGenerator(long seed) {
generator.seed(seed);
}
boost::function<int()> getRandomFunctionInt(int min, int max) {
boost::uniform_int<> uni_dist(min, max);
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > uni(generator, uni_dist);
boost::function<int()> f;
f = boost::bind(uni_dist, generator);
return f;
}
};
int main (int argc, char* argv[]) {
RandomNumberGenerator rng(1729);
boost::function<int()> runif1 = rng.getRandomFunctionInt(0, 1000);
boost::function<int()> runif2 = rng.getRandomFunctionInt(0, 10000);
for (int i=0; i<10; ++i) {
std::cout << runif1() << ", " << runif2() << std::endl;
}
}
输出是:
212, 2121
623, 6226
259, 2590
[...]
有什么方法可以取消函数的关联吗?为了我的实验的可重复性,我想使用单个种子。
【问题讨论】: