1

boost::random を使用して、一様分布に従う確率変数を生成します。

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++)
{
    //source node id
    sourceID = (unsigned int) ( 1 + random() * G.getNumNodes());
    //...
}

しかし、プログラムの実行ごとにシードを異なる方法で初期化する方法が必要です。現在、実行ごとにノードを開始する同じシーケンスを取得しているためです。

4

1 に答える 1

1

boost::random_deviceを使用して、マシンのランダム プール (非決定論的) を使用して、決定論的ジェネレータをシードできます。

#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;
}
于 2013-10-21T18:30:57.463 に答える