使用C++模拟matlab的rand()功能生成0-1的均匀分布的随机数,使用如下程序可实现此功能
static default_random_engine e(time(0));
static uniform_real_distribution<double> u(0, 1);
double tmp = u(e);
cout << tmp << endl;
头文件
#include<random>
#include<ctime>
生成0-1的随机数,运行结果如下
int main()
{
for (int i = 0; i < 10; i++)
{
static default_random_engine e(time(0));
static uniform_real_distribution<double> u(0, 1);//可在此处调整随机数范围
double tmp = u(e);
cout << tmp << endl;
}
system("pause");
return 0;
}
结果