If I don't specify srand(), what seed does rand() use?

前端 未结 5 1531
借酒劲吻你
借酒劲吻你 2020-12-11 18:44

Here is the code:

#include 
#include 
#include 

int main(void)
{
    int r;
    int i;
    for (i = 0; i < 1         


        
相关标签:
5条回答
  • 2020-12-11 18:56
    If rand() is called before any calls to srand() are made, the same sequence shall 
    be generated as when srand() is first called with a seed value of 1.
    

    Ref:

    http://pubs.opengroup.org/onlinepubs/009695399/functions/rand.html

    0 讨论(0)
  • 2020-12-11 18:57

    The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.

    If no seed value is provided, the rand() function is automatically seeded with a value of 1.

    0 讨论(0)
  • 2020-12-11 19:04

    The man pages state the following:

    The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.

    If no seed value is provided, the rand() function is automatically seeded with a value of 1.

    0 讨论(0)
  • 2020-12-11 19:12

    If srand is not called, rand acts as if srand(1) has been called.

    http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.13.html#rand

    0 讨论(0)
  • 2020-12-11 19:15

    The C standard actually stipulates the behaviour documented in the other answers:

    ISO/IEC 9899:2011 §7.22.2.2 The srand function

    ¶2 [...] If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

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