Efficient boost distribution usage

2019-04-02 04:03发布

问题:

(Rephrased the question)

I'm creating a wrapper class for boost normal distribution, and want to make it as efficient as possible.

If I use:

double x = 0.0;
boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor(rng, nd);
for (int i=0; i<20; i++) {
     double x = var_nor();
}

The loop works fine. My concern is that I don't want to be declaring anything unnecessarily as the method gets called many times. I tried splitting up the code and put this line in the constructor:

boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor(rng, nd);

and have a sample method that does this:

     double x = var_nor();
     return x;

But in this case, I get an error saying var_nor() (ie. with no arguments) is not found. Can anyone tell me what's going on with these boost declarations ie. what does the

boost:variate_generate etc.

line actually do in with var_nor? With my limited C++ knowledge, it looks as if var_nor is being defined with two different signatures.

Thanks guys Pete

回答1:

In your code, var_nor is a variable, not a function, so it doesn't have a signature. It represents a variate_generator object that can behave like a function because it supports operator().

In your code, you declare and initialize var_nor at the same time. The rng and nd arguments are passed to the constructor of the variate_generator object.

When you moved the declaration into your class's constructor, you were declaring var_nor as a local variable in the constructor, so it's no wonder it wasn't available elsewhere. For something to be available throughout an entire class, it needs to be a member variable. Declare it private in the class:

class NormalDistribution
{
  boost::random::mt19937 _rng;
  boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor;
public:
  NormalDistribution();
};

Then initialize it in the constructor:

NormalDistribution::NormalDistribution():
  _rng(), var_nor(_rng, boost::normal_distribution<>(0.0, 1.0))
{ }

The _rng member needs to be declared first so that it will be initialized first. The nd parameter can be omitted and replaced with a temporary normal_distribution object passed directly to the var_nor constructor as shown above.

With those changes, you should be able to use the same normal_distribution object over multiple calls to your sample function, or whatever other uses you have for your NormalDistribution class.

In the code you've since deleted from your question, you were confusing variable declarations with function declarations. You declared nd as a function receiving two parameters and returning a normal_distribution. Likewise with var_nor. It was a function when you really wanted an object. You were confused because it happens to be an object that acts like a function, but it's still just an object.



回答2:

OK, working final version using Rob Kennedy's answer, for others that might be interested:

// normaldistribution.h
#ifndef NORMALDISTRIBUTION_H
#define NORMALDISTRIBUTION_H

#include <boost/random.hpp>
#include <boost/random/normal_distribution.hpp>
class NormalDistribution
{
 public:
    NormalDistribution();
    double sample(void);
 private:
    // Use the boost random number generator
    boost::mt19937 rng;
    // Make a variate_generator OBJECT.
    boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > var_nor;
};

#endif // NORMALDISTRIBUTION_H

// normaldistribution.cpp
NormalDistribution::NormalDistribution():
  rng(), var_nor(rng, boost::normal_distribution<>(0.0, 1.0))
{
    std::cout << "Called normal distribution constructor, passing up var_nor" << std::endl;
}

double NormalDistribution::sample(void) {
    double x = var_nor();
    return x;
}

// main.cpp
#include "normaldistribution.h"

int main(int argc, char *argv[])
{
    NormalDistribution *nd = new NormalDistribution();
    for (int i=0; i < 10; ++i)
    {
      double d = nd->sample();
      std::cout << d << std::endl;
    }
    return 0;
}