简体   繁体   中英

How to create vector of variate_generator?

I'm trying to create a vector of boost random number generators.

I've tried creating function pointers to the variate_generators without much luck either.

#include <iostream>
#include <boost/random.hpp>

using namespace std;

int main(int argc, const char *argv[])
{
    const double mean = 0.0;
    const double sigma = 1.0;

    // Setup aliases
    typedef boost::normal_distribution<> NormalDistribution;
    typedef boost::mt19937 RandomNumberGenerator;
    typedef boost::variate_generator<RandomNumberGenerator&, NormalDistribution> NormalGenerator;

    vector<NormalGenerator> gen_vec;

    RandomNumberGenerator rng;
    rng.seed(12345);

    NormalDistribution noise_distribution(mean, sigma);

    NormalGenerator rng_eng(rng, noise_distribution); // create RNG

    gen_vec.push_back(rng_eng);

    return 0;
}

I was hoping to use gen_vec to get a function in order to generate a random number for a given element in the vector. But when I attempt to compile it throws this error.

/usr/include/boost/random/detail/pass_through_engine.hpp:27: error: non-static reference member \u2018main(int, const char**)::RandomNumberGenerator& boost::random::detail::pass_through_engine<main(int, const char**)::RandomNumberGenerator&>::_rng\u2019, can't use default assignment operator

I'm guessing that's because I'm not packing the variate_generators correctly.

Is what I'm trying to do correct much less possible?

At first, your example code does compile without any error message (GCC 8.2.1, boost 1.69).

But, don't do that. Just use boost::variate_generator<>::distribution() to set a new distribution.

NormalDistribution noise_distribution(mean, sigma);
NormalDistribution other_distribution(5.0, 2.0);
NormalGenerator rng_eng(rng, noise_distribution); // create RNG
// ... generate RNG using noise_distribution
rng_eng.distribution() = other_distribution;
// ... generate RNG using other_distribution
rng_eng.distribution() = noise_distribution;
// ... generate RNG using noise_distribution again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM