Boost logo

Boost :

Subject: Re: [boost] Problem with boost::random seed
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-01-25 11:45:01


AMDG

On 1/24/2011 11:29 AM, Anton Skvorts wrote:
> Testing boost::random with the following code
>
> boost::mt19937 gener(1);
> boost::normal_distribution<> normal(0,1);
> boost::variate_generator<boost::mt19937&,boost::normal_distribution<> >
> rng(gener, normal);
> cout<< rng()<< endl;
> cout<< rng()<< endl;
> cout<< rng()<< endl;
> gener.seed(2);
> cout<< rng()<< endl;
> cout<< rng()<< endl;
> cout<< rng()<< endl;
>
> I'm getting unexpected values after gener.seed(2)
> -2.971829031 1.706951063 -0.430498971
>
> -2.282022417 x
> -0.5887503675
> 0.2504171986
>
> The value -2.282022417 is the forth number in the sequence originated by
> gener(1) and not the first number in the sequence originated by gener.seed(2)
> as one would expect (this number is actually -0.5887503675). If I reseed
> next with gener.seed(3)it works fine, but if after that I reseed again with
> gener.seed(1) I'll get the same problem, and so on and off...
>
> How can I reseed the random number generator to overcome this problem?
>

This is because normal_distribution uses Box-Muller,
which generates pairs of normal variates. You need
to call rng.distribution().reset() to clear the cached
result.

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk