使用的是C++中默认的随机数引擎,引擎由编译器决定;
随机数库的组成是由:随机数引擎和随机数分布类型组成;
#include <random>
default_random_engine generator(time(NULL));//引擎,发生器
uniform_int_distribution<unsigned> distribution(0, 9); //均匀分布
//生成随机数
for (int i = 0; i < 100; i++)
{
cout << distribution(generator) << " ";
}
//统计显示:也可以利用显示*的方法查看每一个数出现多少次
vector<unsigned> vals(9);//9个0
for (int i = 0; i != 500;i++)
{
unsigned v = lround(distribution(generator));
if (v < vals.size())
{
++vals[v];
}
}
for (int j = 0; j != vals.size();++j)
{
cout << j << ": " << string(vals[j], '*') << endl;
}
//梅森旋转成器
// mersenne_twister_engine constructor
#include <iostream>
#include <chrono>
#include <random>
// obtain a seed from the system clock:
unsigned seed1 = system_clock::now().time_since_epoch().count();
mt19937 generator(seed1); // mt19937 is a standard mersenne_twister_engine
cout << "A time seed produced: " << generator() << endl;
//当然,也可以添加分布类型,参照上面的均匀分布即可