miwaiwai

导航

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;

}

 

posted on 2023-01-10 07:47  米歪歪  阅读(29)  评论(0编辑  收藏  举报