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 mt19937<http://www.boost.org/doc/libs/1_45_0/doc/html/boost/mt19937.html>with 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 <time.h> #include <iostream> using namespace std; using namespace boost; mt19937 gen; // time_t gen = time(0); int roll_die() { uniform_int<> dist(1, 6); variate_generator<mt19937&, uniform_int<> > die(gen, dist); //boost::variate_generator<time_t, boost::uniform_int<> > die(gen, dist); return die(); } int main() { for (int i = 0; i < 5; i++) cout << roll_die() << endl; return 0; } So, I would be glad, if someone could fix this for me, to have a different seed (depending on system time) at each start. Thanks!

I think you have to use the time as argument for the generator mt19937 gen(time(0)); 2011/1/24 Zoltán Lengyel <katzlengyel@gmail.com>
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 mt19937<http://www.boost.org/doc/libs/1_45_0/doc/html/boost/mt19937.html>with 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 <time.h>
#include <iostream> using namespace std; using namespace boost;
mt19937 gen; // time_t gen = time(0);
int roll_die() { uniform_int<> dist(1, 6); variate_generator<mt19937&, uniform_int<> > die(gen, dist); //boost::variate_generator<time_t, boost::uniform_int<> > die(gen, dist); return die(); }
int main() { for (int i = 0; i < 5; i++) cout << roll_die() << endl;
return 0; }
So, I would be glad, if someone could fix this for me, to have a different seed (depending on system time) at each start. Thanks! _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

#include "uniform_int.hpp" #include "boost/random/variate_generator.hpp" #include "boost/random/mersenne_twister.hpp" #include <time.h> #include <iostream> using namespace std; using namespace boost; time_t T = time(0); mt19937 gen(T); int roll_die() { uniform_int<> dist(1, 6); variate_generator<mt19937&, uniform_int<> > die(gen, dist); return die(); } int main() { for (int i = 0; i < 5; i++) cout << roll_die() << endl; return 0; } Thanks for your help, it works this way. On 24 January 2011 15:33, Hernán Leoni <leoni.hernan@gmail.com> wrote:
I think you have to use the time as argument for the generator mt19937 gen(time(0));
2011/1/24 Zoltán Lengyel <katzlengyel@gmail.com>
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 mt19937<http://www.boost.org/doc/libs/1_45_0/doc/html/boost/mt19937.html>with 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 <time.h>
#include <iostream> using namespace std; using namespace boost;
mt19937 gen; // time_t gen = time(0);
int roll_die() { uniform_int<> dist(1, 6); variate_generator<mt19937&, uniform_int<> > die(gen, dist); //boost::variate_generator<time_t, boost::uniform_int<> > die(gen, dist); return die(); }
int main() { for (int i = 0; i < 5; i++) cout << roll_die() << endl;
return 0; }
So, I would be glad, if someone could fix this for me, to have a different seed (depending on system time) at each start. Thanks! _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi, On Monday, 24. January 2011 14:33:23 Zoltán Lengyel wrote:
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#bo ost_random.tutorial.generating_integers_in_a_range This tutorial tells: "We use mt19937<http://www.boost.org/doc/libs/1_45_0/doc/html/boost/mt19937.html>w ith 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 <time.h>
#include <iostream> using namespace std; using namespace boost;
mt19937 gen; // time_t gen = time(0);
int roll_die() { uniform_int<> dist(1, 6); variate_generator<mt19937&, uniform_int<> > die(gen, dist); //boost::variate_generator<time_t, boost::uniform_int<> > die(gen, dist); return die(); }
int main() { for (int i = 0; i < 5; i++) cout << roll_die() << endl;
return 0; }
So, I would be glad, if someone could fix this for me, to have a different seed (depending on system time) at each start. Thanks!
I use this approach: #include <ctime> #include <time.h> #include <sys/time.h> struct timeval tv; gettimeofday(&tv, 0); long int seed; seed = (tv.tv_sec * 1000000) + tv.tv_usec; // Set the seed gen.seed( seed ); I am however not sure how exact it is (high precision timer?!) and if there isn't probably a better way to get a fresh seed at every execution. It might be that rapid subsequent executions will get the same seed and thus will produce the same results. It is no surprise that your code does not work or even compile, as you define a global random generator and at the same time you want to define a time-variable with the same variable name. So with the code above, you will not need a global time variable to set the seed. You could e.g. set it before the for- loop when calling roll_die(). Besides that, asking for "if someone could fix this for me" is generally not a good idea ;) It suggests that someone should do your work instead of you... Best, Cedric

* Message by -Cedric Laczny- from Mon 2011-01-24:
I use this approach:
#include <ctime> #include <time.h> #include <sys/time.h>
struct timeval tv; gettimeofday(&tv, 0); long int seed; seed = (tv.tv_sec * 1000000) + tv.tv_usec;
// Set the seed gen.seed( seed );
I am however not sure how exact it is (high precision timer?!) and if there isn't probably a better way to get a fresh seed at every execution. It might be that rapid subsequent executions will get the same seed and thus will produce the same results.
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.

AMDG On 1/24/2011 12:57 PM, Lasse Kliemann wrote:
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.
boost::random_device seed_gen; boost::mt19937 gen(seed_gen()); should have the same effect and is more portable. In Christ, Steven Watanabe

On Monday, 24. January 2011 22:11:55 Steven Watanabe wrote:
AMDG
On 1/24/2011 12:57 PM, Lasse Kliemann wrote:
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.
boost::random_device seed_gen; boost::mt19937 gen(seed_gen());
should have the same effect and is more portable.
Nice. Thank you.
In Christ, Steven Watanabe _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Best, Cedric

On Monday, 24. January 2011 21:57:59 Lasse Kliemann wrote:
* Message by -Cedric Laczny- from Mon 2011-01-24:
I use this approach:
#include <ctime> #include <time.h> #include <sys/time.h>
struct timeval tv; gettimeofday(&tv, 0); long int seed; seed = (tv.tv_sec * 1000000) + tv.tv_usec;
// Set the seed
gen.seed( seed );
I am however not sure how exact it is (high precision timer?!) and if there isn't probably a better way to get a fresh seed at every execution. It might be that rapid subsequent executions will get the same seed and thus will produce the same results.
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.
Interesting suggestion. Thank you. Best, Cedric
participants (5)
-
Cedric Laczny
-
Hernán Leoni
-
Lasse Kliemann
-
Steven Watanabe
-
Zoltán Lengyel