mardi 25 octobre 2022

Is it allowed for srand(0) to have the same effect as srand(1)?

Is it allowed for srand(0) to have the same effect as srand(1)?

C11, 7.22.2.2 The srand function (empahasis added):

The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand.

However, in glibc srand(0) has the same effect as srand(1):

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

Hence, the same sequence of pseudo-random numbers is returned by subsequent calls to rand, which is confusing.

Extra: We see that in MSVC srand(0) does not have the same effect as srand(1).




Aucun commentaire:

Enregistrer un commentaire