|
Boost : |
Subject: Re: [boost] Problem with boost::random seed
From: Anton Skvorts (anton.skvorts_at_[hidden])
Date: 2011-01-27 21:18:51
I'm still having some problems with boost::random, obviously because I
don't fully understand all that s going on... Read the documentation
and your post but still cannot find what is wrong with my code
bellow:
extern "C" __declspec(dllexport) double normalReseed(double mean,
double sigma, uint32_t x){
boost::normal_distribution<> normal(mean,sigma);
boost::variate_generator<boost::mt19937&,boost::normal_distribution<>
> rng(gen, normal);
rng.engine().seed(x);
rng.distribution().reset();
return rng();
}
boost::mt19937 gen;
boost::normal_distribution<> normal(0,1);
boost::variate_generator<boost::mt19937 &,boost::normal_distribution<>
> die(gen, normal);
cout << rng() << endl;
cout << rng() << endl;
cout << rng() << endl;
cout << normalReseed(0, 1, 0) << endl;
cout << rng() << endl;
cout << rng() << endl;
cout << rng() << endl;
cout << normalReseed(0, 1, 1) << endl;
cout << rng() << endl;
cout << rng() << endl;
cout << rng() << endl;
I get the following output:
0.2134360137 // #1 sequence normal(0,1) with seed( )
-0.4955802821 // #2 sequence normal(0,1) with seed( )
1.57537639 // #3 sequence normal(0,1) with seed( )
-1.278008129 // #1 sequence normal(0,1) with seed(0) OK
-1.059203928 // #4 sequence normal(0,1) with seed( ) x
-0.418455056 // #3 sequence normal(0,1) with seed(0)
-1.882579026 // #4 sequence normal(0,1) with seed(0)
-2.971829031 // #1 sequence normal(0,1) with seed(1) OK
-0.430498971 // #3 sequence normal(0,1) with seed(1) x
-2.282022417 // #4 sequence normal(0,1) with seed(1)
0.5236573494 // #5 sequence normal(0,1) with seed(1)
Possibly are again cached results from Box-Muller algorithm the reason
for this pattern but still cannot see why we get these results,
neither how to overcome this problem
Anton Skvorts,
2011/1/25 Steven Watanabe <watanabesj_at_[hidden]>
>
> 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
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk