lundi 10 août 2015

There are several different types of random number generators defined in <random> like minstd_rand0 or mt19937, but they don't share a common base even though they have the exact same member functions (like operator() or min and max). Thus, to use such a generator with a distribution like uniform_int_distribution the distribution's function also has to be a template. And if I wanted to store a generator in a different class I would have to make that class a template class with the generator as template parameter like this:

template <typename generator_t>
class foo
{
    generator_t gen;
}

A way to generalize the use of those generators would be to abstract them into a function object of type std::function<int()> and then write a method which returns such a function object generator given an ordinary generator from <random> like so (optionally with a distribution included):

template <typename generator_t>
std::function<int()> create_generator(generator_t& gen)
    return [gen](){
        return gen();
    };
}

Similarly one could also create a class that abstracts all other functions available in the ordinary generators. In both cases it would unnecessarily lower the performance because of these indirections.

Since I generate a huge amount of random numbers I need the process to be as efficient as possible while also enabling me to swap out generators or distributions without having to rewrite any code. I don't like the option of making all the classes that use random number generation template classes with the generator type as template parameter just because the generators defined in <random> don't have a common base. Is there any other way to accomplish this?




Aucun commentaire:

Enregistrer un commentaire