我打算将std::uniform_real_distribution
与一些非内置浮点型类型一起使用,例如 half_float::half
或 boost::multiprecision::float128
。但是我得到的是
从g++ 5.2开始。这是示例程序(使用g++ -std=c++11 -fext-numeric-literals test.cpp -o test
编译):
#include <random>
#include <boost/multiprecision/float128.hpp>
#include <half.hpp>
template<typename Float>
void test()
{
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<Float> rnd(Float(1),Float(10));
}
int main()
{
test<float>();
test<double>();
test<long double>();
test<boost::multiprecision::float128>(); // doesn't compile
test<half_float::half>(); // doesn't compile
}
那么,应该如何为此类自定义类型生成均匀分布的随机实数呢?有没有办法重新发明轮子的方法?
最佳答案
从here可以看到,uniform_real_distribution
定义为:
template< class RealType = double >
class uniform_real_distribution;
其中
RealType
是:看来您的编译器明确禁止使用自定义类型作为解决(让我说) Not Acceptable 类型的解决方案。因此,我会说您没有机会使其正常工作。
关于c++ - 如何在不重新发明轮子的情况下为自定义类型生成均匀分布的随机实数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41777551/