|
Boost Users : |
Subject: [Boost-users] boost::random distribution is not working within a custom class
From: Oliver Fochler (janitor048_at_[hidden])
Date: 2009-07-08 11:29:57
Hey there,
I'm having a problem with the boost::random library and I can't figure
out what's going on, so maybe you guys might help me out.
Here's the scenario (code below): I wrote a small class that should
encapsulate a random number generator (floating point, uniform in
[0,1) ) so that I ultimately can switch between different approaches
(boost and non-boost) without the rest of my code ever caring.
Within this class I have a boost::mt19937 object and a pointer to a
boost::uniform_01<boost::mt19937&, double> as private members. Within
the constructor I actually create an instance of uniform_01 via new
and initialize it with the mt19937 RNG. Now within the constructor
(see cout statement) the ran_boost->operator()() or (*ran_boost)()
work perfectly and yield some random double between 0 and 1.
However, when using the randomNumberGenerator object created in
main.cpp I'm struck with segfault. Doing some backtrace it turns out
that it fails with
in boost::random::mersenne_twister<unsigned int, 32, 624, 397, 31,
2567483615u, 11, 7, 2636928640u, 15, 4022730752u, 18,
3346425566u>::operator() (
this=0x89485ed18949ed31) at
/home/fochler/usr/include/boost/random/mersenne_twister.hpp:275
If I try to use global object ran2 (declared extern in random2.h and
defined in random2.cpp) I also get a segfault, this time with
in
boost::random::detail::pass_through_engine<boost::random::mersenne_twister<unsigned
int, 32, 624, 397, 31, 2567483615u, 11, 7, 2636928640u, 15,
4022730752u, 18, 3346425566u>&>::base (this=0x0) at
/home/fochler/usr/include/boost/random/detail/pass_through_engine.hpp:42
42 base_type& base() { return helper_type::ref(_rng); }
I don't really know (a) what is going on and what I've messed up and
(b) whether these two errors (using the local and the global object)
are connected. It would be great if somebody could help me out.
Cheers,
Oliver
Some minimal example
-----------------
random2.h
-----------------
#ifndef RANDOM_NG_H
#define RANDOM_NG_H
#include <stdint.h>
#include <boost/random.hpp>
class randomNumberGenerator
{
public:
randomNumberGenerator( const uint32_t s );
double operator()() { return ran_boost->operator()(); }
private:
boost::mt19937 rng_mt_boost;
boost::uniform_01<boost::mt19937&, double> * ran_boost;
};
extern randomNumberGenerator ran2;
#endif
-----------------
random2.cpp
-----------------
#include <iostream>
#include <boost/random.hpp>
#include "random2.h"
randomNumberGenerator ran2;
randomNumberGenerator::randomNumberGenerator( const uint32_t s )
{
boost::uniform_01<boost::mt19937&, double> * ran_boost = new
boost::uniform_01<boost::mt19937&, double>( rng_mt_boost );
rng_mt_boost.seed( s );
std::cout << ran_boost->operator()() << std::endl;
}
-----------------
main.cpp
-----------------
#include "random2.h"
int main(int argc, char **argv)
{
randomNumberGenerator ran3( 1234567 );
double a = ran2();
double b = ran3();
return 0;
}
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