Problem with boost::random seed

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? Thanks in advance

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

Many thanks for your advice. Now it works fine! 2011/1/25 Steven Watanabe <watanabesj@gmail.com>
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

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@gmail.com>
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

AMDG On 1/27/2011 6:18 PM, Anton Skvorts wrote:
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:
Notice that you have two separate variate_generators, one in the main code, and one for each call to normalReseed. Each normal_distribution object has its own cache. When you call reset, you need to make sure that you call it on the distribution that you are using.
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; <snip>
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
In Christ, Steven Watanabe

Oh, although the code was run with just one variate_generator, I transcribed in my post one line of the code erroneously: where is die(gen, normal) it should be rng(gen, normal) Anyway I got that strange output using just one variate_generator everywhere: variate_generator<std::mt19937 &,std::normal_distribution<> > rng(gen, normal); Steven, does your comment still applies in this case? 2011/1/28 Steven Watanabe <watanabesj@gmail.com>:
AMDG
On 1/27/2011 6:18 PM, Anton Skvorts wrote:
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:
Notice that you have two separate variate_generators, one in the main code, and one for each call to normalReseed. Each normal_distribution object has its own cache. When you call reset, you need to make sure that you call it on the distribution that you are using.
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; <snip>
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
In Christ, Steven Watanabe _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

AMDG On 1/27/2011 7:10 PM, Anton Skvorts wrote:
Oh, although the code was run with just one variate_generator, I transcribed in my post one line of the code erroneously:
where is die(gen, normal) it should be rng(gen, normal)
Anyway I got that strange output using just one variate_generator everywhere: variate_generator<std::mt19937&,std::normal_distribution<> > rng(gen, normal);
Steven, does your comment still applies in this case?
I can't reproduce your problem. Here's the code I tested with #include <boost/random/mersenne_twister.hpp> #include <boost/random/normal_distribution.hpp> #include <boost/random/variate_generator.hpp> #include <iostream> using namespace std; template<class G, class T> void reseed(G& gen, const T& t) { gen.engine().seed(t); gen.distribution().reset(); } int main() { boost::mt19937 gen; boost::normal_distribution<> normal(0,1); boost::variate_generator< boost::mt19937 &, boost::normal_distribution<> > rng(gen, normal); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; reseed(rng, 0); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; reseed(rng, 1); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; } In Christ, Steven Watanabe

Steven, Thank you so much! Your code works perfectly, In case it would be useful to reproduce my previous (wrong) results I'm sending the full code bellow. I used gcc to compile it. #include <iostream> #include "boost/random.hpp" using namespace std; boost::mt19937 gen; extern "C" __declspec(dllexport) double normalSeed(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(); }
int main() { cout.precision (10); cout << "\nBoost::Random Test, 0.12e" << endl; boost::normal_distribution<> normal(0,1); boost::variate_generator<boost::mt19937 &,boost::normal_distribution<> > rng(gen, normal); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; cout << "Function call seed(0)\n" << normalSeed(0, 1, 0) << endl; cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; cout << "Function call seed(1)\n" << normalSeed(0, 1, 1) << endl; cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; return 0; } 2011/1/28 Steven Watanabe <watanabesj@gmail.com>:
AMDG
On 1/27/2011 7:10 PM, Anton Skvorts wrote:
Oh, although the code was run with just one variate_generator, I transcribed in my post one line of the code erroneously:
where is die(gen, normal) it should be rng(gen, normal)
Anyway I got that strange output using just one variate_generator everywhere: variate_generator<std::mt19937&,std::normal_distribution<> > rng(gen, normal);
Steven, does your comment still applies in this case?
I can't reproduce your problem. Here's the code I tested with
#include <boost/random/mersenne_twister.hpp> #include <boost/random/normal_distribution.hpp> #include <boost/random/variate_generator.hpp> #include <iostream>
using namespace std;
template<class G, class T> void reseed(G& gen, const T& t) { gen.engine().seed(t); gen.distribution().reset(); }
int main() { boost::mt19937 gen; boost::normal_distribution<> normal(0,1); boost::variate_generator< boost::mt19937 &, boost::normal_distribution<> > rng(gen, normal); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; reseed(rng, 0); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; reseed(rng, 1); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; }
In Christ, Steven Watanabe _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Hello Steven, I get the following from you code: 0.213436 -0.49558 1.57538 -1.27801 -0.404742 -0.418455 -1.88258 -2.97183 1.70695 -0.430499 -2.28202 Shouldn't they be in [0, 1]? I'm using WinXP + VS2008. Thanks Max
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: Friday, January 28, 2011 1:12 PM To: boost@lists.boost.org Subject: Re: [boost] Problem with boost::random seed
AMDG
On 1/27/2011 7:10 PM, Anton Skvorts wrote:
Oh, although the code was run with just one variate_generator, I transcribed in my post one line of the code erroneously:
where is die(gen, normal) it should be rng(gen, normal)
Anyway I got that strange output using just one variate_generator everywhere: variate_generator<std::mt19937&,std::normal_distribution<> > rng(gen, normal);
Steven, does your comment still applies in this case?
I can't reproduce your problem. Here's the code I tested with
#include <boost/random/mersenne_twister.hpp> #include <boost/random/normal_distribution.hpp> #include <boost/random/variate_generator.hpp> #include <iostream>
using namespace std;
template<class G, class T> void reseed(G& gen, const T& t) { gen.engine().seed(t); gen.distribution().reset(); }
int main() { boost::mt19937 gen; boost::normal_distribution<> normal(0,1); boost::variate_generator< boost::mt19937 &, boost::normal_distribution<> > rng(gen, normal); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; reseed(rng, 0); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; reseed(rng, 1); cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; cout << rng() << endl; }
In Christ, Steven Watanabe _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

AMDG On 2/9/2011 5:50 AM, Max wrote:
Hello Steven,
I get the following from you code:
0.213436 -0.49558 1.57538 -1.27801 -0.404742 -0.418455 -1.88258 -2.97183 1.70695 -0.430499 -2.28202
Shouldn't they be in [0, 1]?
No. Normal variates can be any real number. In Christ, Steven Watanabe

Shouldn't they be in [0, 1]?
No. Normal variates can be any real number.
In Christ, Steven Watanabe
Oh, my fault. The parameters in boost::normal_distribution<> normal(0,1) are the mean and sigma of the normal distribution, not the range of the generated numbers. Thanks B/Rgds Max
participants (3)
-
Anton Skvorts
-
Max
-
Steven Watanabe