DIfference in output of srand() on windows gcc and expected output for C programming?

前端 未结 2 1161
不知归路
不知归路 2020-12-21 20:59

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 compile

相关标签:
2条回答
  • 2020-12-21 21:48

    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.

    0 讨论(0)
  • 2020-12-21 21:49

    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().

    0 讨论(0)
提交回复
热议问题