c随机数函数
生成一个随机数
1.添加一个随机数种子srand(time(NULL))
2.添加头文件
<stdlib.h>
<time.h>
3.生成随机数 int n=rand() %100(对100取余,生成随机数的范围0-99)
#include<stdlib.h> #include<time.h> #include<stdio.h> #include <unistd.h> int main(){ srand(time(NULL)); int num; for(;;){num=rand()%100; printf("%d\n",num); sleep(2); } system("pause"); return 0; }
浙公网安备 33010602011771号