Windows gcc上srand()的输出与C编程的预期输出有区别吗? [英] DIfference in output of srand() on windows gcc and expected output for C programming?

查看:118
本文介绍了Windows gcc上srand()的输出与C编程的预期输出有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么Windows 10上Sublime Text 3中我的gcc编译器的输出与预期输出有区别?如果是这样,我如何更改编译器以使其运行相同?

i would like to know why there is a difference in the output from my gcc compiler in Sublime Text 3 on Windows 10 and the expected output? If so how can i change the compiler such that it operates the same?

这是预期的输出,当我使用 https://repl.it/repls/InternalSeveralEntropy时,我会收到此输出 gcc 4.6.3

This is the expected output, which i can receive when utilising https://repl.it/repls/InternalSeveralEntropy with 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 

但是,这是我使用多个gcc版本, 5.4.0 6.3.0 8.1.0 甚至是我收到的输出 4.6.3 .

However, this is the output i receive using multiple gcc versions, 5.4.0, 6.3.0, 8.1.0 and even 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

我尝试搜索,但最接近的理解是它与srand()的功能有所不同吗?以下是我的代码.

I have tried searching and the closest i have come to understand is that it has soemthing to do with srand() functioning differently? Attached below is my code.

    #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;

}

推荐答案

根据C11标准:

7.22.2.1 rand函数

7.22.2.1 The rand function

... 脚注:无法保证所产生的随机序列的质量,并且已知某些实现会产生具有令人痛苦的非随机低阶位的序列.有特殊要求的应用程序应使用已知足以满足其需求的生成器.

... Footnote: There are no guarantees as to the quality of the random sequence produced and some implementations are known to produce sequences with distressingly non-random low-order bits. Applications with particular requirements should use a generator that is known to be sufficient for their needs.

这句话清楚地说明了randsrand是实现定义的,并且程序的顺序(用于评分!)也是实现定义的.

This quote makes clear that rand and srand are implementation defined, and the sequence of the program (used for grading!) is also implementation defined.

这篇关于Windows gcc上srand()的输出与C编程的预期输出有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆