想输出一百个随机数,但只输出了一个

img


想输出一百个随机数,但只输出了一个随机数,不知道如何改正,能帮我改正一下吗?

因为你的rand函数只调用了一次,你可以将rand函数放在for循环里面试试。

img


将这个放到循环内。不然只取到一个随机数。然后你又不停的赋值同一个数字。所以只会输出一个值


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int a;
srand((unsigned)time(NULL));

for(int i = 0;i<100;i++)
{
    a=rand()%100+1;
    printf("%d\n",a);
}

return 0;
}