简介
随机数生成一般以时间为种子,但是当前时间一般以s为结束,所以要想得到多个基于不同时间的随机数,那么一定要加上sleep
code
#include <iostream>
#include "common.h"
#include <windows.h>
using namespace std;
int main()
{
for (int i = 0; i < 100; i++) {
Sleep(1000);
cout << generateRandom(50, 100) << endl;
}
system("pause");
return 0;
}
-----------------------------------
#include "common.h"
// start 开始的数字
// end 结束的数字
// 闭区间进行描述
int generateRandom(int start, int end) {
unsigned seed; // Random generator seed
// Use the time function to get a "seed” value for srand
seed = time(0);
srand(seed);
int number = (rand() % (start - end + 1)) + start;
return number;
}
参考链接
https://blog.csdn.net/hopegrace/article/details/102916787
http://c.biancheng.net/view/1352.html