1

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.

Kapoios
  • 688
  • 7
  • 22
  • 2
    [`boost::random_device`](http://www.boost.org/doc/libs/1_54_0/doc/html/boost/random/random_device.html) – dyp Oct 21 '13 at 16:51
  • 2
    @RichardJ.RossIII If you're already using `boost::random`, you might as well use `boost::random_device`, which is specified to get the best possible truly random value. For the seed---`boost::random_device` is likely to be far too slow for large amounts of random values. – James Kanze Oct 21 '13 at 17:00
  • 2
    Usage of `boost::random_device` to provide a seed: `boost::random_device rd; boost::mt19937 gen(rd);` Note: the `random_device` of boost is not available for all platforms (in C++11 it is always implemented, but can be a PRNG if a non-deterministic RNG is not available). – dyp Oct 21 '13 at 17:24

1 Answers1

1

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;
}
teeks99
  • 3,585
  • 2
  • 30
  • 38