How does calling srand more than once affect the quality of randomness?

前端 未结 5 1322
借酒劲吻你
借酒劲吻你 2020-12-06 19:21

This comment, which states:

srand(time(0)); I would put this line as the first line in main() instead if calling it multiple times (

5条回答
  •  抹茶落季
    2020-12-06 19:43

    The seed determines what random numbers will be generated, in order, i.e. srand(1), will always generate the same number on the first call to rand(), the same on the second call to rand() and so on.

    In other words, if you re-seeded with the same seed before each rand() invocation, you'd generate the same random number every single time.

    So successive seeding with time(0), during a single second, will mean all your random numbers after re-seeding are actually the same number.

提交回复
热议问题