Random seed at runtime

前端 未结 3 1314
梦谈多话
梦谈多话 2021-01-05 03:35

How can I generate different random numbers at runtime?

I\'ve tried

srand((unsigned) time(0));

But it seems to get me a random numb

3条回答
  •  灰色年华
    2021-01-05 03:42

    You need to call srand once per program execution. Calling rand updates the internal state of the random number generator, so calling srand again actually resets the random state. If less than a second has passed, time will be the same and you will get the same stream of random numbers.

提交回复
热议问题