Boost random number in OO program

Posted on 2006-05-29
Medium Priority
Last Modified: 2012-06-21
I've written C++ code for many years, in research rather than production system.  I find that a bit hard to mix the modern generic C++ coding style into OO code.  The Boost library is a good example.   In specific, I am trying to use the random number generator in Boost.  My usual practice is like this
class simulator{
    Random *m_rand;
    simulator(Random *rand):m_rand(rand){}
    void runSimulation(){        
         //--- the rest of the calc.
         //--- the rest of the calc.
But, in Boost, the syntax is like
variate_generator<someGenerator, someDist > sampler(rng, dist);
then, you can use the function object sampler() to generate random number.

When put that back to my simulation class, it creates quite a bit of problem.  I have difficulty putting the declaration of the sampler into the construtor.
class simulator{
variate_generator<someGenerator, someDist > sampler;
//--the rest of the code.
The above code is not valid as variate_generator has no default constructor.  Of course, no one really wants to put the sampler initialisation to the global scope....

Here is the sample program from wikipeda.org that I actually refer to:
int main(){

      // select random number generator
      mt19937 rng;
      // seed generator with #seconds since 1970
      rng.seed(static_cast<unsigned> (std::time(0)));

      // select desired probability distribution
      normal_distribution<double> dist(0, 1);
      // bind random number generator to distribution, forming a function
      variate_generator<mt19937&, normal_distribution<double> > 

        return 0;

Question by:oldjst
1 Comment
LVL 19

Accepted Solution

mrwad99 earned 375 total points
ID: 16785155
>> The above code is not valid as variate_generator has no default constructor.

Yeah, that is correct.  In general, whenever you have an object of another class as a member of your main class, and that object requires parameters in its construction, you should have a pointer to that object, and 'new' it in your main class's constructor.  the sub objects that require parameters should have those parameters passed in the constructor of the main class.  For example,

class simulator{

     simulator( double dNormalDistributorVal );  // Note: constructor now has parameter


     variate_generator< mt19937&, normal_distribution<double> >* m_pSampler;

     normal_distributor<double>* m_pNormalDistributor;

     mt19937 rng;  // Requires no parameters; can be a stack variable

     //.. other code


simulator::simulator( double dNormalDistributorVal )
     m_pNormalDistributor = new normal_distributor<dNormalDistributorVal>;
     m_pSampler = new variate_generator< rng, m_pNormalDistributor >;

     delete m_pNormalDistributor;
     delete m_pSampler;


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Make the most of your online learning experience.
Loops Section Overview

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question