C++的随机数函数rand是一个伪随机数,从固定的初始种子计算得出。所以每次运行获取到的随机数序列是相同的。要解决这一问题,需要设置一个随机数种子,一般用当前时间作为种子
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int n = rand() % 100;
cout << n << endl;
}
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int n = rand() % 100;
cout << n << endl;
}