Boost logo

Boost Users :

From: Anne-Gert Bultena (list-aap_at_[hidden])
Date: 2006-06-06 08:53:50


You could use static local variables, but that's not threadsafe. On the other hand, Since a
constant value is written, it doesn't need to be a problem, even in a multi threaded environment.

example:

int generate()
{
    static int number = 10;
    return number++;
}

The above function will return the next number from 10 onward. The initialization of number will
only occur at the first call, due to the variable being static.

Maybe this is a solution you can use,

agb

> 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
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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