Boost logo

Boost Users :

Subject: [Boost-users] Writing a function to use a generic random number generator
From: Joseph Wakeling (joseph.wakeling_at_[hidden])
Date: 2010-04-23 11:42:55


Hello all,

A question regarding Boost's random number generators.

Here's a simple example use-case:

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

using namespace std;
using namespace boost;

void generate_random_numbers(vector<double> &x,
                             mt19937 &rng)
{
    BOOST_FOREACH(double &z, x)
        z = uniform_real<double> (0.0, 5.0) (rng);
}

int main()
{
    mt19937 rng;
    vector<double> x;

    x.resize(1000);

    generate_random_numbers(x,rng);

        for(size_t i=0;i!=10;++i)
                cout << x.at(i) << endl;

    // and now do something fun with x ... :-)

    return 0;
}
/////////////////////////////////////////////////////////

The question is, how do I go about getting generate_random_numbers() to
use an _arbitrary_ random number generator instead of having to be
mt19937? In particular, is there a way to do this without needing to
have a template function?

Thanks & best wishes,

    -- Joe


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