一种方法改变升压随机的::种子在每个不同的程序运行

编程入门 行业动态 更新时间:2024-10-15 04:27:32
本文介绍了一种方法改变升压随机的::种子在每个不同的程序运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用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; }

更多推荐

一种方法改变升压随机的::种子在每个不同的程序运行

本文发布于:2023-11-27 10:43:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:种子   程序   方法   在每个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!