我正在尝试在C ++主程序中生成一些随机数。
我使用的解决方案是
#include <random>
#include <iostream>
#include <cmath>
using namespace std;
// Function to generate a random double number between two limits
double getRandomDouble(double lower_bound, double upper_bound)
{
std::uniform_real_distribution<double> unif(lower_bound,upper_bound);
std::default_random_engine re;
double a_random_double = unif(re);
return a_random_double ;
}
// Funtion to print a random number
void print()
{
cout << getRandomDouble(0,10) << endl;
}
int main()
{
for (int i=0; i<10; i++)
{
print();
}
return 0;
}
运行该程序时,得到以下输出:
1.31538 1.31538 1.31538 1.31538 1.31538 1.31538 1.31538 1.31538 1.31538 1.31538
那是打印10次的相同数字。
每次调用函数 print()时,应该如何修改代码以获得不同的随机数?