Boost logo

Boost Users :

Subject: Re: [Boost-users] [Random] Passing an arbitrary uniform random number generator to a function
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2010-04-26 16:47:41


AMDG

Neal Becker wrote:
> Joseph Wakeling wrote:
>
>> Suppose that I have a function that takes a uniform random number
>> generator as an argument, e.g.
>>
>> double foo(mt19937 &urng)
>> {
>> double x = uniform_real<double> (0.0, 10.0) (urng);
>> double y = uniform_real<double> (5.0, 7.0) (urng);
>>
>> return (x*y);
>> }
>>
>> Is there a way to make the choice of random number generator arbitrary
>> (i.e. not limited to mt19937) without making the function a template?
>> Alternatively, is there a better way of passing around the basic uniform
>> random number generator?
>>
>> The reason for the no-template rule is that I would like to pass a URNG
>> to a virtual function in a class, and I would really not like to have to
>> make the class itself a template dependent on the type of URNG.
>>
>> Can someone recommend a solution?
>>
>> Thanks and best wishes,
>>
> member func can be template without class class being template.
>

A virtual function can't be a template.

You might be able to use something like this:

template<class ResultType>
class runtime_generator {
public:
    ResultType min() const { return min_; }
    ResultType max() const { return max_; }
    ResultType operator()() { return impl(); }
private:
    ResultType min_, max_;
    boost::function<ResultType()> impl;
};

In Christ,
Steven Watanabe


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