如题
原代码
#include <random>
using std::cout;
int main()
{
for (int i = 0; i < 5; i++)
{
cout << rand() << '\n';
}
}
运行三次结果输出:
三次运行结果一模一样。
解决方法
在所有循环之前,加上srand(unsigned(time(NULL)));
即可:
#include <random>
using std::cout;
int main()
{
srand(unsigned(time(NULL)));
for (int i = 0; i < 5; i++)
{
cout << rand() << '\n';
}
}
运行结果
搞定。