#include <stdlib.h>
#include<stdio.h>
#include<time.h>
#define randomize() srand((unsigned)time(NULL)) //定义一个宏
int main(void)
{
int i;
printf("Ten random numbers from 0 to 99/n/n");
randomize();
for(i=0; i<100; i++)
{
printf("%d/n", rand() % 100);
sleep(1);
}
return 0;
}
<运行>
root@farsight:/mnt/hgfs/E/yinhui/12.3/msg# gcc tttt.c -o tttt
root@farsight:/mnt/hgfs/E/yinhui/12.3/msg# ./tttt
Ten random numbers from 0 to 99
87
80
68
39
39
53
14
96
46
本文展示了一个使用C语言生成随机数的简单程序示例。该程序通过调用srand和rand函数来生成从0到99的随机整数,并演示了如何在程序中初始化随机数生成器以及如何进行循环输出。

589

被折叠的 条评论
为什么被折叠?



