首页 > 其他分享 >cpp generate template random number

cpp generate template random number

时间:2023-02-22 11:12:11浏览次数:36  
标签:std min int max random template cpp generate

#include <iostream>
#include <random>
 template<typename T>
    T gen_random(T min,T max)
    {
        std::random_device rd;
        std::mt19937_64 mt(rd());
        std::uniform_int_distribution<T> uid(min,max);
        return uid(mt);
    }
 
    template<typename T>
    void template_random(T min,T max,int len)
    {
        for(int i=0;i<len;i++)
        {
            std::cout<<i+1<<","<<gen_random<T>(min,max)<<"\t";
        }
        std::cout<<std::endl;
    }


//main.cpp
void template_rand(int len)
{
   template_random<std::uint32_t>(0,UINT32_MAX,len);
}

 

标签:std,min,int,max,random,template,cpp,generate
From: https://www.cnblogs.com/Fred1987/p/17143651.html

相关文章