美文网首页
5.6.3 随机数

5.6.3 随机数

作者: Jianbaozi | 来源:发表于2019-10-07 17:13 被阅读0次
    #include<iostream>
    #include<vector>
    #include<random>
    using namespace std;
    template<typename T>class RandT{
    public:
    RandT(T low,T high):dist{low,high}{}
    T operator()(){ return dist(re);}
    private:
    default_random_engine re;
    uniform_int_distribution<>dist;
    };
    int main(){
        RandT<int> rnd{0,4};
        vector<int>histogram(5);
        for(int i=0;i!=200;++i){
            ++histogram[rnd()];
        }
        for(int i=0;i!=5;++i){
            cout<<i<<'\t';
            for(int j=0;j!=histogram[i];++j)
            cout<<"*";
            cout<<endl;
        }
        system("pause");
        return 0;
    }
    
    F:\Codes\test>a.exe
    0       ****************************************
    1       ************************************
    2       *********************************************
    3       *************************************
    4       ******************************************
    请按任意键继续. . .
    
    

    相关文章

      网友评论

          本文标题:5.6.3 随机数

          本文链接:https://www.haomeiwen.com/subject/auatuctx.html