random library: initializaing a generator.
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
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Tue, Jun 06, 2006 at 02:53:50PM +0200, Anne-Gert Bultena wrote:
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++; }
This is not thread-safe when two different threads call generate at the same time. number++ both reads and writes to number. -- Carlo Wood <carlo@alinoe.com>
dvir schirman 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.
If you just want the generator to be initialized once for each object of my_class that is created, this will do the trick: class my_class { protected: typedef boost::mt19937 generator_type; typedef boost::normal_distribution<> normal_distribution_type; typedef boost::variate_generator<generator_type &, normal_distribution_type> normal_random_type; generator_type m_generator; normal_distribution_type m_normal_distribution; normal_random_type m_normal_random; public: my_class() : m_generator(42u), m_normal_distribution(5,10), m_normal_random(m_generator, m_normal_distribution) { ; // do nothing } double noise() { return m_normal_random(); } }; If you want the generator to be initialized only once total (no matter how many instances of the class are created), you can use this: class my_class_static { protected: typedef boost::mt19937 generator_type; typedef boost::normal_distribution<> normal_distribution_type; typedef boost::variate_generator<generator_type &, normal_distribution_type> normal_random_type; static generator_type m_generator; static normal_distribution_type m_normal_distribution; static normal_random_type m_normal_random; public: static double noise() { return m_normal_random(); } }; You never actually need an instance of the class my_class_static since the noise function is static. The static member m_generator will be initialized only once, at program startup. (Note that this is not thread-safe and would need to be protected with a lock.) David Testing code: static void Test_my_class() { my_class mine; for(unsigned int i=0 ; i<9 ; ++i) { double d = mine.noise(); std::cout << d << " "; } std::cout << std::endl; } static void Test_my_class_static() { for(unsigned int i=0 ; i<9 ; ++i) { double d = my_class_static::noise(); std::cout << d << " "; } std::cout << std::endl; } int main(int argc, char* argv[]) { std::cout << "Testing my_class - next two lines should be same\n"; Test_my_class(); Test_my_class(); std::cout << "Testing my_class_static - next line same as above, last line different\n"; Test_my_class_static(); Test_my_class_static(); return 0; }
participants (4)
-
Anne-Gert Bultena
-
Carlo Wood
-
David Walthall
-
dvir schirman