Boost logo

Boost Users :

Subject: [Boost-users] [random] streamable state of normal_distribution to ostream not accurate
From: Oei, YC (oei.yungchin_at_[hidden])
Date: 2008-11-06 15:59:26


Dear list members,

I'm using the Boost Random Number Library, and am now trying to store
the state of an instance of normal_distribution by means of its <<
operator. It doesn't however seem to work: three of its data members
are definitely not outputting properly on my system.

I can reproduce the problem with this test file:

-----

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/normal_distribution.hpp>
#include <iomanip>
#include <iostream>
#include <limits>

using namespace boost;
using namespace std;

int main() {
   mt19937 rng;
   normal_distribution<> gauss;
   variate_generator<mt19937&, normal_distribution<> > var(rng, gauss);

   cerr << setprecision(numeric_limits<double>::digits10 * 2);
   for (size_t i = 0; i != 5; ++i) {
       cerr << var() << endl; //prints a random number
       cerr << gauss << endl; //dumps object internal state
   } //for
} //main

-----

which, on Ubuntu 8.04 amd64 (gcc 4.2.4, boost 1.34.1) produces this output:

0.21343601372288967255030911474
0 1 0 6.95335189373114898041666608493e-310 6.9533518937319394854500120794e-310
-0.495580282118778303601658308253
0 1 0 6.95335189373114898041666608493e-310 6.9533518937319394854500120794e-310
1.57537639010992913668474102451
0 1 0 6.95335189373114898041666608493e-310 6.9533518937319394854500120794e-310
-1.05920392793252360341682560829
0 1 0 6.95335189373114898041666608493e-310 6.9533518937319394854500120794e-310
1.83926543714107570970384131215
0 1 0 6.95335189373114898041666608493e-310 6.9533518937319394854500120794e-310

...and I get similar output on an OpenSuse 10.2 x86_64 system (gcc
4.1.2, boost 1.33.1).

If I understand the code in boost/random/normal_distribution.hpp
correctly, the third member in the stream represents _valid and should
alternate between 0 and 1, and the last two values should definitely
not stay the same for five iterations.

Can you tell me if I'm doing anything wrong?

(And, slightly offtopic, what should I set the stream-precision at to
ensure exact reloading of the floating point data?)

Many thanks for reading and thinking about this,
Yung-Chin Oei


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