本文介绍了一种方法改变升压随机的::种子在每个不同的程序运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用boost ::随机生成如下均匀分布的随机变量。
I use boost::random to generate a random variable that follows uniform distribution.
boost::mt19937 gen(2014/*time(NULL)*/); boost::uniform_real<> dist(0, 1); boost::variate_generator<boost::mt19937&, boost::uniform_real<> > random(gen, dist);通过该变量,我选择统一在每个不同的实验不同的起始图节点。
With this variable,I uniformly choose a different starting graph node in every different experiment.
for(unsigned int i=0; i < numQueries; i++) { //source node id sourceID = (unsigned int) ( 1 + random() * G.getNumNodes()); //... }但我需要一种方法来在我的程序的每个不同运行不同的初始化种子,我得到在每一个不同的运行从现在开始节点的顺序相同。
But I'm in need of a way to initialize the seed differently in each different run of my program, as I get the same sequence of starting nodes in every different run now.
推荐答案您可以使用的的boost :: random_device 使用机器的随机池(这是不确定的)种子的确定性发生器。
You can use boost::random_device to use the machine's random pool (which is non-deterministic) to seed your deterministic generator.
#include <boost/random.hpp> #include <boost/random/random_device.hpp> #include <iostream> unsigned int numQueries = 10; int main(int argc, char* argv[]) { boost::random_device dev; boost::mt19937 gen(dev); //boost::mt19937 gen(2014/*time(NULL)*/); boost::uniform_real<> dist(0, 1); boost::variate_generator<boost::mt19937&, boost::uniform_real<> > random(gen, dist); for(unsigned int i=0; i < numQueries; i++) { // I don't have G, so I'm just going to print out the double //sourceID = (unsigned int) ( 1 + random() * G.getNumNodes()); double sourceID = (random()); std::cout << sourceID << std::endl; } return 0; }更多推荐
一种方法改变升压随机的::种子在每个不同的程序运行
发布评论