What‘s the difference between srand(1) and srand(0)

后端 未结 7 2226
广开言路
广开言路 2020-12-30 02:23

I just found out the hard way that srand(1) resets the PRNG of C(++) to the state before any call to srand (as defined in the reference). However,

7条回答
  •  生来不讨喜
    2020-12-30 03:16

    How glibc does it:

    around line 181 of glibc/stdlib/random_r.c, inside function __srandom_r

      /* We must make sure the seed is not 0.  Take arbitrarily 1 in this case.  */
      if (seed == 0)
        seed = 1;
    

    But that's just how glibc does it. It depends on the implementation of the C standard library.

提交回复
热议问题