简体   繁体   English

在 C++ 中模板化随机数生成器

[英]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.我应该有uniform_int_distribution<int> ,但我需要一个可以工作的随机数生成器。 I mean I could generate int and divide them by 10^n to get a float but I dont like the elegance of it.我的意思是我可以生成 int 并将它们除以10^n以获得浮点数,但我不喜欢它的优雅。

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谢谢您的帮助

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. std::uniform_int_distribution只为一些基本整数类型定义, std::uniform_real_distribution只为基本浮点类型定义。

You could choose between those with std::conditional_t您可以在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.不幸的是,有许多整数类型不能与std::uniform_int_distribution一起使用,因此我们必须枚举允许的类型。

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));
}

A solution that works well for me (with C++17):一个适合我的解决方案(使用 C++17):

#include <random>
#include <type_traits>

template<typename T, typename R = std::mt19937_64>
T random(const T& min, const T& max)
{
  R rng{std::random_device{}()};
  if constexpr (std::is_integral<T>::value) {
    std::uniform_int_distribution<T> dist(min, max);
    return dist(rng);

  } else if (std::is_floating_point<T>::value) {
    std::uniform_real_distribution<T> dist(min, max);
    return dist(rng);
  } else {
    // If T is unsupported type...
    return T{};
  }
}

An example of use with Google bench: https://github.com/bensuperpc/vector/blob/c7fd274be0c667009bba3c150fd5c8082496bbc4/test/source/random_bench.cpp#L12与 Google bench 一起使用的示例: https ://github.com/bensuperpc/vector/blob/c7fd274be0c667009bba3c150fd5c8082496bbc4/test/source/random_bench.cpp#L12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM