我想知道为什么在Windows 10的Sublime Text 3中我的gcc编译器的输出与预期输出有差异?如果是这样,我如何更改编译器以使其运行相同?
这是预期的输出,我可以在使用https://repl.it/repls/InternalSeveralEntropy和gcc 4.6.3时收到
The set of numbers are:
1 4 3 3 7 2 3 2 9 9 10 9 5 2 10 7 2 3 1 1
Run Code Online (Sandbox Code Playgroud)
然而,这是我收到使用多个版本的GCC,输出5.4.0,6.3.0,8.1.0,甚至4.6.3.
The set of numbers are:
2 2 8 6 8 2 3 8 4 6 10 5 9 2 8 10 7 2 1 6
Run Code Online (Sandbox Code Playgroud)
我尝试过搜索,最接近我已经理解的是,它与srand()的功能有什么不同?下面是我的代码.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int N = 20, DATA[20];
int i; //< you local variables here >
srand(454646); //don't change this line! Will be used in autograding... You may fail test cases if change this
for (i = 0; i < N; i++){ //< generate rand numbers and store here in DATA array >
DATA[i] = rand()%10 + 1;
}
printf("The set of numbers are:\n");
for (i = 0; i < N; i++){
printf("%d ", DATA[i]);
}
printf("\n");
//< write using FOR loops to determine MODE and print >
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从C11标准:
7.22.2.1兰特函数
...脚注:无法保证产生的随机序列的质量,并且已知一些实现产生具有令人沮丧的非随机低阶位的序列.具有特殊要求的应用应使用已知足以满足其需求的发电机.
这个引用清楚地表明了rand并且srand是实现定义的,程序的顺序(用于评分!)也是实现定义的.