boost::random distribution is not working within a custom class

View: New views
5 Messages — Rating Filter:   Alert me  

boost::random distribution is not working within a custom class

by Bugzilla from janitor048@googlemail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: boost::random distribution is not working within a custom class

by Steven Watanabe-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

AMDG

Oliver Fochler wrote:

> Some minimal example
>
> -----------------
> random2.h
> -----------------
> <snip>
> class randomNumberGenerator
> {
>    <snip>
>  private:
>    boost::uniform_01<boost::mt19937&, double> * ran_boost;
> };
>
> -----------------
> random2.cpp
> -----------------
>
> <snip>
>
> 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 );
>  

You're creating a local variable here, not initializing the
member ran_boost.

>  std::cout << ran_boost->operator()() << std::endl;
> }
>  

In Christ,
Steven Watanabe

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: boost::random distribution is not working within a custom class

by Bugzilla from janitor048@googlemail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



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 );
 

You're creating a local variable here, not initializing the
member ran_boost.

Oh, what a stupid mistake!  Thanks for pointing it out so quickly.

Cheers,
Oliver


_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: boost::random distribution is not working within a custom class

by Ovanes Markarian :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

In addition to Steven's remark: May be it relates to minimal example, but why do you need a member pointer here? And why don't you delete it?

I think that would be a better approach:

-----------------
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(); }

 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 )
  : rng_mt_boost_()
  , ran_boost_(rng_mt_boost_)
{
 rng_mt_boost_.seed( s );
 std::cout << ran_boost_() << std::endl;
}



Regards,
Ovanes



On Wed, Jul 8, 2009 at 6:46 PM, Steven Watanabe <watanabesj@...> wrote:
AMDG

Oliver Fochler wrote:


You're creating a local variable here, not initializing the
member ran_boost.

In Christ,
Steven Watanabe


_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: boost::random distribution is not working within a custom class

by Bugzilla from janitor048@googlemail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


2009/7/8 Ovanes Markarian <om_boost@...>
Hi!

In addition to Steven's remark: May be it relates to minimal example, but why do you need a member pointer here? And why don't you delete it?

I think that would be a better approach:

Yes, the thing about not deleting the member pointer was indeed due to the minimal example - I omitted some stuff and with it the destructor. But you are right, it should work perfectly with a normal member. I think I'll change that for reasons of clarity - don't really know why I made it a pointer in the first place, probably just came into my mind.


_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users