|
Boost Users : |
Subject: [Boost-users] Random: How to get a different seed?
From: Zoltán Lengyel (katzlengyel_at_[hidden])
Date: 2011-01-24 08:33:23
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#boost_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>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 list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net