1using the time as a seed;
要获得一个随机整数,需要一个函数,以及给他一个初值。
函数为srand(),需要头文件 ,一般情况下我们使用当前系统的时间的
秒数作为初值的,因为这样的值一般情况下是不会相同。需要头文件
#include // For time()
#include // For srand() and rand()
2 给一个获取随机三位数的例子
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
. . .
srand(time(0)); // Initialize random number generator.,把当前时间作为随机数的种子
. . .
r = (rand() % 1000) + 1; //获得随机数