Search code examples
c++randomencapsulation

Encapsulate c++ Random Number Generator


I'm building something that requires me to

template<D>
class DistributionAdapter {
public:
    /**
     * @return number generated by the distribution function.
     */
    virtual D operator()(RANDOM_NUMBER_GENERATOR& rng) = 0;
};

RANDOM_NUMBER_GENERATOR is supposed to represent the class of random number generator in c++, either std::random_device or a pseudo random number generator. Can someone tell me how should I approach this, I don't know if random number generator in c++ have a common base type


Solution

  • Section § 26.5.1.3 of the standard describes the requirements for random number generators.

    In particular, a generator must support the function call operator :

    g() T Returns a value in the closed interval [ G::min() , G::max() ] . amortized constant

    So, although there is no base class shared by every single generator, the standard guarantees that the operator() will be present in each of them : you can call rng() in your function.