随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、QQ技术交流群(183198395)。
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
int main(void){
//使用默认种子值产生随机数,每次产生的都一样
cout << rand() << " " << rand() << " " << rand() << endl;
//使用新种子产生随机数,每次都不一样
srand((unsigned int)time(0));
cout << rand() << " " << rand() << " " << rand() << endl;
return 0;
}