均匀的随机分布“基类”包括:对于int和double? [英] Uniform random distribution "base class" for both int and double?

查看:82
本文介绍了均匀的随机分布“基类”包括:对于int和double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个用随机数填充列表的函数,并且根据列表项的类型,它应该生成整数或浮点数。到目前为止,我已经提出了以下代码,并且可以正常工作:

I'm trying to make a function that will fill a list with random numbers, and based on the type of the list items it should generate either integer or floating point numbers. So far I've come up with the following code, and it works:

template <typename T>
void generateRandom(list<T>& numberList){
    default_random_engine randomGenerator(random_device{}());

    if( typeid(T) == typeid(int) ){
        uniform_int_distribution<T> distribution(1000, 2000);
        auto myGenerator = bind(distribution, randomGenerator);
        generate(numberList.begin(), numberList.end(), myGenerator);
    }
    else if( typeid(T) == typeid(double) ){
        uniform_real_distribution<T> distribution(1000.0, 2000.0);
        auto myGenerator = bind(distribution, randomGenerator);
        generate(numberList.begin(), numberList.end(), myGenerator);
    }
    else{
        return;
    }

}

但是,我并不是真的对此解决方案感到满意。只是感觉应该有某种方法可以分解除IF语句之外的所有内容。

However, I'm not really satisfied with this solution. It just feels like there should be some way to break out everything but the actual distribution from that IF statement. Something like:

template <typename T>
void generateRandom(list<T>& numberList){
    default_random_engine randomGenerator(random_device{}());

    X distribution;
    if( typeid(T) == typeid(int) )
        distribution = uniform_int_distribution<T>(1000, 2000);
    else if( typeid(T) == typeid(double) )
        distribution = uniform_real_distribution<T>(1000.0, 2000.0);
    else
        return;

    auto myGenerator = bind(distribution, randomGenerator);
    generate(numberList.begin(), numberList.end(), myGenerator);
}

(X是此处缺少的占位符)

(X is a placeholder for what's missing here)

但是由于 uniform_int_distribution uniform_real_distribution 没有通用的基类,那有可能吗?我曾尝试过使用函数指针和函子,但似乎从未超越过IF语句,这很烦人。

But since uniform_int_distribution and uniform_real_distribution don't have a common base class, how would that be possible? I have tried playing around with function pointers and functors, but I never seem to get past that IF-statement, and it's annoying. Can this be solved in a more elegant way?

注意:这是学校作业的一部分,我们必须使用 generate()< random> 函数和模板来生成随机数。我认为我的原始解决方案是可以接受的,我只是不喜欢IF语句中那些重复的代码行的外观...:)

NOTE: It's part of a school assignment, and we have to use generate() and the <random> functions and templates to generate the random numbers. I think my original solution is acceptable, I just don't like the look of those duplicate lines of code in the IF-statement... :)

推荐答案

您可以使用:

template <typename T>
using my_distribution = std::conditional_t<std::is_integral<T>::value,
                   std::uniform_int_distribution<T>,
                   std::conditional_t<std::is_floating_point<T>::value,
                                      std::uniform_real_distribution<T>,
                                      void>
                   >;

然后

template <typename T>
void generateRandom(list<T>& numberList){
    default_random_engine randomGenerator(random_device{}());
    my_distribution<T> distribution(1000, 2000);
    auto myGenerator = bind(distribution, randomGenerator);
    generate(numberList.begin(), numberList.end(), myGenerator);
}

这篇关于均匀的随机分布“基类”包括:对于int和double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆