#include <stdlib.h> //rand()
#include <time.h> //time(0)
#pragma mark - 随机数,值会重复
void randNum()
{
time_t a = time(0); //系统时间
srand((unsigned)time(0)); //随机数种子,如果参数不变,rand的随机数序列不变,一般使用系统时间
for(int i=0;i<10;i++){
//如果不加上srand函数,则每次的值都一样
printf("%5d",rand()%13+1); //rand()产生0到32767的随机数,模13就得到0到12的随机数,加1就是1到13的随机数
}
printf("\n");
}
输出:
12 8 4 8 5 4 10 10 13 13