Home > Software engineering >  templating a random number generator in c
templating a random number generator in c

Time:10-21

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

CodePudding user response:

std::uniform_int_distribution is only defined for some fundamental integer types, and std::uniform_real_distribution is only defined for the fundamental floating point types.

You could choose between those with std::conditional_t

Unfortunately there are a number of integral types that are not usable with std::uniform_int_distribution, so we have to enumerate the allowed ones.

template <typename> struct has_uniform_distribution : std::false_type;

template <std::floating_point T> struct has_uniform_distribution<T> : std::true_type;

template <> struct has_uniform_distribution<short> : std::true_type;
template <> struct has_uniform_distribution<unsigned short> : std::true_type;

template <> struct has_uniform_distribution<int> : std::true_type;
template <> struct has_uniform_distribution<unsigned int> : std::true_type;

template <> struct has_uniform_distribution<long> : std::true_type;
template <> struct has_uniform_distribution<unsigned long> : std::true_type;

template <> struct has_uniform_distribution<long long> : std::true_type;
template <> struct has_uniform_distribution<unsigned long long> : std::true_type;

template <typename T>
concept uniform_distribution = has_uniform_distribution<T>::value;

template <uniform_distribution T> // or sfinae over has_uniform_distribution in C   earlier than C  20
T aleaGenVal(T min, T max)
{
    std::random_device dev;
    std::mt19937 rng(dev());

    using dist_t = std::conditional_t<
        std::is_integral_v<T>, 
        std::uniform_int_distribution<T>, 
        std::uniform_real_distribution<T>
    >;

    dist_t dist(min,max);
    return dist(rng);
}

template <typename T>
T aleaGenVal(T min, T max) = delete;

Alternatively, we could define it for all arithmetic types, by using the widest generator type, and narrowing the result

template <std::arithmetic T> // or sfinae over std::is_arithmetic in C   earlier than C  20
T aleaGenVal(T min, T max)
{
    std::random_device dev;
    std::mt19937 rng(dev());

    using dist_t = std::conditional_t<
        std::is_integral_v<T>, 
        std::conditional_t<
            std::is_signed_v<T>, 
            std::uniform_int_distribution<long long>, 
            std::uniform_int_distribution<unsigned long long>> 
        std::uniform_real_distribution<T>>;

    dist_t dist(min,max);
    return static_cast<T>(dist(rng));
}
  • Related