此 C 代码应生成 100 万个随机数。多次编译代码时,我使用 srand() 来解决伪随机生成问题。我认为理论上这段代码应该可以工作,但它似乎遗漏了一些可能导致溢出的东西。知道为什么它在执行过程中停止工作吗?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
FILE *ofp;
int k=0, i=1000000;;
int array[i];
ofp=fopen("output.txt","w");
while (ofp==NULL)
{
printf("File is corrupted or does not exist, \nplease create or fix the file and press enter to proceed");
getchar();
}
srand(time(NULL));
rand();
for (k=0; k<1000000; k++)
{
array[k] = rand()%100;
fprintf(ofp, "%d\n", array[k]);
}
return 0;
}