$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: David Klein (d.klein_at_[hidden])
Date: 2006-06-06 10:51:21
boost-users-bounces_at_[hidden] wrote:
> Hi
> 
> I am using boost/random library in order to create a normal
> distributed noise. I want to call my my_class::noise() functions
> several times, 
> but I want to initialize the generator only once.
> I tried to declare and initialize the generator at the
> constructor of my_class, but then my_class::noise() doesn't
> recognized the generator. I wrote it the following way:
> 
> my_class::my_class()
> {
> boost::mt19937 gen(42u);
> 
> }
> 
> my_class::noise()
> 
> {
> 
> boost::normal_distribution<
> 
> float> n_d(5, 10); //n_d(mean,stdev)
> 
> boost::variate_generator<boost::mt19937&,
> boost::normal_distribution<float> > normal(gen, n_d);
> 
> return normal();
> 
> }
> 
> 
> 
> I guess I need to declare the generator at the header, and
> only initialize it at the constructor, but I don't know how to do it.
> 
> Thanks for your help.
> Dvir
hi dvir,
this is untested, but should do what you want.
class my_class
{
        boost::mt19937 gen_;
public:
        my_class( void ) :
                gen_(42u)
        {
        }
        void
        noise( void )
        {
                boost::normal_distribution<float> n_d(5, 10);
//n_d(mean,stdev)
                boost::variate_generator<boost::mt19937&,
boost::normal_distribution<float> > normal(gen_, n_d);
         }
};
HTH dave