templating a random number generator in c++(用C++制作随机数生成器模板)
本文介绍了用C++制作随机数生成器模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我的代码错误。我应该有uniform_int_distribution<int>,但是我需要一个随机数生成器,它可以工作在任何类型的代码中。
我的意思是,我可以生成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);
}
感谢您的帮助
推荐答案
std::uniform_int_distribution仅为一些基础整数类型定义,std::uniform_real_distribution仅为基础浮点类型定义。
您可以在std::conditional_t
遗憾的是,有许多整型无法与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;
或者,我们也可以为所有算术类型定义它,方法是使用最宽的生成器类型并缩小结果范围
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));
}
这篇关于用C++制作随机数生成器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:用C++制作随机数生成器模板
基础教程推荐
猜你喜欢
- 提升 ASIO 流缓冲 2021-01-01
- c++ STL设置差异 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
