1

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?

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 

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

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;

}
r3mainer
  • 23,981
  • 3
  • 51
  • 88
Mervin
  • 11
  • 4
  • Irrelevant: You need to seed `srand` for generating decent randomness. – WedaPashi Nov 01 '18 at 09:21
  • Hmm...maybe they should generate the same sequence. Using VC2008 it generated the correct sequence. – Paul Ogilvie Nov 01 '18 at 09:31
  • 1
    I believe `replit` uses Linux, and thus perhaps may use a different implementation for `rand` than that of ming-gw's gcc running on Windows. I wouldn't focus on the differences of your randomness with theirs. – jacob Nov 01 '18 at 09:36
  • 1
    @WedaPashi Please note that it may be useful in some contexts (like testing) to produce the same pseudo random sequence every time. – Bob__ Nov 01 '18 at 09:55
  • @Bob__: Ah, I didn't realise it. Thanks! – WedaPashi Nov 01 '18 at 10:03
  • The source code for `rand()` is here: https://sourceware.org/git/?p=glibc.git;a=tree;f=stdlib – r3mainer Nov 01 '18 at 10:09
  • 4
    *//don't change this line! Will be used in autograding... You may fail test cases if change this* Wow. Does whoever created this problem know that the data from `rand()` is implementation-dependent? If so, did that person specify the exact implemention (compiler, OS, C runtime library, etc) that you have to use? If not, that person doesn't understand the problem. – Andrew Henle Nov 01 '18 at 10:54

2 Answers2

6

From C11 standard:

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.

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

Community
  • 1
  • 1
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
4

As part 7.22.2.1 of C11 standard mentions, output of rand() function is implementation defined, so it may change based on each implementation (or even versions of compiler).

The only thing that is guaranteed is this:

Returns a pseudo-random integer value between ​0​ and RAND_MAX (0 and RAND_MAX included).

This means that even uniform distribution is not guaranteed too.

If you need a generate pseudo-random numbers generator which is guaranteed to create same sequence all the time (for each seed), you need to write that functions yourself. One way to write such a function is using LFSR, or you can check implementation of a rand() function for a specific version and add it to your code as a your application specific random function like myrand().

Afshin
  • 8,839
  • 1
  • 18
  • 53