형식을 기준으로 난수 생성을 위해 VS2010에서 템플릿을 사용하려고합니다. char
/int
/long
등 (정수형) 전체 범위를 커버 번호를 반환하기위한 BaseTypeRandomizer의 객체를 생성, 불행하게도float/double에 대한 작은 범위의 난수, 정수형 (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);
}
}
};
하지만, 수레와 복식에 대한 그들은하지 않습니다 : 나는 아래의 코드를 사용하고 있습니다. 부유물은 모두 1e+37
에서 9e+38
사이이며, 복식은 1e+307
에서 2e+308
까지입니다 (또는 적어도 모두 해당 지역에 있음). VS 디버거에서 dist
개체를 검사하면 올바른 제한이 표시되지만 Values
벡터는 훨씬 작은 범위의 숫자로 채워집니다.
한계가 제대로 작동하지 않는 이유에 대해 누구나 알고 있습니까?
그럼 난수 생성을 위해 float/double 형식의 전체 범위를 어떻게 사용할 수 있습니까? 감사합니다, Oren –
@OrenSarid : 이전의 대답은 여기서 진짜 문제를 놓쳤습니다. 내 편집 된 답변보기 – interjay