Boost logo

Boost Users :

From: Daniel James (daniel_james_at_[hidden])
Date: 2006-09-10 02:20:42


Jim Ingram wrote:
> Some more information:
>
> I'm using gcc 4.0.2 on Ubuntu, with the latest available version of Boost
> (for Ubuntu anyway. It's 1.32.0+1.33.0-cvs20050727).
That's the version from Ubuntu Breezy, 1.33.1 is in Dapper.

> I'm writing a class that is supposed to simulate a six-sided die.
> It compiles and everything works, except seeding the mt19937. Every time I
> try testing this class, I get the same numbers, no matter what value I call
> my seed method with.
The problem is that 'variate_generator' stores a copy of the engine, not
a reference. So this works:

#include <boost/random.hpp>

class D6 {
        typedef boost::mt19937 engine_t;
        typedef boost::uniform_int<> dist_t;
        typedef boost::variate_generator<engine_t, dist_t> gen_t;
public:
        D6() : die(engine_t(), dist_t(1,6)) { }
        explicit D6(boost::uint32_t s) : die(engine_t(s), dist_t(1,6)) { }

        int roll() { return die(); }

        void seed(boost::uint32_t s) {
                die.engine().seed(s);
        }

private:
        gen_t die;
};


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