随机函数的用法
#include <stdlib.h> //srand、rand函数声明
#include <time.h> //time函数声明
在程序开始时调用一次srand(time(0)); //用当前时间做为随机数种子,以使程序每次运行时产生的随机数序列不相同。
使用rand()返回一个非负的随机整数,要生成一个在区间 [a, b) 的随机整数赋值给 x,可用以下公式:
x = rand() % (b-a) + a;
随机函数的用法
#include <stdlib.h> //srand、rand函数声明
#include <time.h> //time函数声明
在程序开始时调用一次srand(time(0)); //用当前时间做为随机数种子,以使程序每次运行时产生的随机数序列不相同。
使用rand()返回一个非负的随机整数,要生成一个在区间 [a, b) 的随机整数赋值给 x,可用以下公式:
x = rand() % (b-a) + a;