1、
#include <stdlib.h>
#include <random>
#include <iostream>
using namespace std;
void main()
{
std::tr1::mt19937 eng; // a core engine class:Mersenne Twister generator std::cout<<"产生正态分布的10个随机数:"<<endl;
std::tr1::normal_distribution<double> dist;
for (int i=; i<; i++)
{
std::cout<<dist(eng)<<endl;
} std::cout<<"\n产生均匀分布的在1到52之间的五个整数随机数:"<<endl;
std::tr1::uniform_int<int> unif(, );
for (int i=; i<; i++)
{
std::cout<<unif(eng)<<endl;
}
system("pause");
}
上述代码每次运行时生成的随机数序列固定不变:
2、
#include <random>
#include <iostream>
#include <time.h>
using namespace std;
void main()
{
std::tr1::mt19937 eng; // a core engine class:Mersenne Twister generator
eng.seed((unsigned int)time(NULL)); // reseed base engine 设置种子用#include <time.h>, 不能用#include <time> std::cout<<"产生正态分布的10个随机数:"<<endl;
std::tr1::normal_distribution<double> dist;
for (int i=; i<; i++)
{
std::cout<<dist(eng)<<endl;
} std::cout<<"\n产生均匀分布的在1到52之间的五个整数随机数:"<<endl;
std::tr1::uniform_int<int> unif(, );
for (int i=; i<; i++)
{
std::cout<<unif(eng)<<endl;
}
system("pause");
}