首页 > 编程语言 >vc++ get random via random_device,mt19937

vc++ get random via random_device,mt19937

时间:2022-09-07 09:12:18浏览次数:92  
标签:arr via uid mt19937 int max random len

 

#include <ctime>
#include <iostream>
#include <random>

using namespace std;

static random_device rd;
static mt19937 mt{ rd() };

template<typename T>
void getRandomArray(T* arr, int len, T min, T max)
{
    uniform_int_distribution<T> uid(min, max);
    for (int i = 0; i < len; i++)
    {
        arr[i] = uid(mt);
    }
}


template<typename T>
void printArray(T* arr, int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << i << "," << arr[i] <<"\t";
    }
    cout<<endl<<endl << "Finished in " << __FUNCTION__ << endl;
}

void arrayDemoT(int len);

int main(int args,char**argv)
{      
    int len = atoi(argv[1]);
    arrayDemoT(len);
}
 
void arrayDemoT(int len)
{
    uint32_t* arr = new uint32_t[len];
    getRandomArray<uint32_t>(arr, len, 0, UINT32_MAX);
    printArray<uint32_t>(arr, len);
    delete[]arr;
}

template<typename T>
void getRandomArray(T* arr, int len, T min, T max)
{
    uniform_int_distribution<T> uid(min, max);
    for (int i = 0; i < len; i++)
    {
        arr[i] = uid(mt);
    }
}

 

 

 

 

 

 

标签:arr,via,uid,mt19937,int,max,random,len
From: https://www.cnblogs.com/Fred1987/p/16664064.html

相关文章