Random: How to get a different seed?

Hello,
I am trying to make a simple program that can generate random integers in
range, using boost::uniform_int.
http://www.boost.org/doc/libs/1_45_0/doc/html/boost_random/tutorial.html#boo...
This tutorial tells:
"We use mt19937http://www.boost.org/doc/libs/1_45_0/doc/html/boost/mt19937.htmlwith
the default seed as a source of randomness. The numbers produced will
be the same every time the program is run. One common method to change this
is to seed with the current time (std::time(0) defined in ctime)."
I have trouble getting this common method compile and work. Here's a tiny
program that works, with the same seed at every run (like what the tutorial
does), I have commented out my desperate tries to make the seed change.
#include "uniform_int.hpp"
#include "boost/random/variate_generator.hpp"
#include "boost/random/mersenne_twister.hpp"
#include

#include "uniform_int.hpp"
#include "boost/random/variate_generator.hpp"
#include "boost/random/mersenne_twister.hpp"
#include

Hi, On Monday, 24. January 2011 14:33:23 Zoltán Lengyel wrote:
I use this approach:
#include <ctime>
#include

* Message by -Cedric Laczny- from Mon 2011-01-24:
I use /dev/urandom for this, like so (code fragments): FILE *dev_urandom; uint32_t seed; if((dev_urandom = fopen("/dev/urandom", "r")) == NULL) throw /* error */ if(fread(&seed, sizeof(seed), 1, dev_urandom) == 0) throw /* error */ if(fclose(dev_urandom) != 0) throw /* error */ boost::mt19937 gen(seed); But maybe there are even better ways.
participants (5)
-
Cedric Laczny
-
Hernán Leoni
-
Lasse Kliemann
-
Steven Watanabe
-
Zoltán Lengyel