Boost logo

Boost :

Subject: Re: [boost] Problem with boost::random seed
From: Anton Skvorts (anton.skvorts_at_[hidden])
Date: 2011-01-28 11:30:46


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_at_[hidden]>:
> 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
>


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