我想在VS2010中使用模板来根据类型创建随机数。我使用下面的代码:浮点数/双精度浮点数的小范围,适用于整型(C++,VS2010)
template<class BaseT>
struct distribution
{ // general case, assuming T is of integral type
typedef std::tr1::uniform_int<BaseT> dist_type;
};
template<>
struct distribution<float>
{ // float case
typedef std::tr1::uniform_real<float> dist_type;
};
template<>
struct distribution<double>
{ // double case
typedef std::tr1::uniform_real_distribution<double> dist_type;
};
template<class BaseT>
class BaseTypeRandomizer
{
public:
BaseTypeRandomizer() : mEngine(std::time(0))
{
}
void CreateValues(std::vector<BaseT>& Values, size_t nValues)
{
typedef typename distribution<BaseT>::dist_type distro_type;
std::random_device Engine;
distro_type dist(std::numeric_limits<BaseT>::min(), std::numeric_limits<BaseT>::max());
for (size_t iVal = 0; iVal < nValues; ++iVal)
{
Values[iVal] = dist(Engine);
}
}
};
不幸的是,char
/int
/long
等(整型)返回一个覆盖整个范围内的数字创造BaseTypeRandomizer的对象,但和双精度浮点数他们不这样做。漂浮在1e+37
到9e+38
之间,而双打是1e+307
到2e+308
(或至少所有人都在那附近)。检查VS调试器中的dist
对象显示了正确的限制,但Values
矢量被填充了更小的数字范围。
有没有人有一个想法,为什么限制不能正常工作?
那么我怎样才能使用全部范围的浮动/双打随机数生成? 谢谢, Oren –
@OrenSarid:我以前的答案在这里错过了真正的问题。看到我编辑的答案。 – interjay