mercredi 20 octobre 2021

templating a random number generator in c++

I know my code is wrong. I should have uniform_int_distribution<int>, but I need a random number generator that works whatever the type is. I mean I could generate int and divide them by 10^n to get a float but I dont like the elegance of it.

template <class T>
T aleaGenVal(const T &min,const T &max)
{
    std::random_device dev;
    std::mt19937 rng(dev());

    std::uniform_int_distribution<T> dist(min,max);
    return dist(rng);
}

thank you for your help




Aucun commentaire:

Enregistrer un commentaire