Random seed at runtime

前端 未结 3 1301
梦谈多话
梦谈多话 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:48

    srand is used to seed the random number generator. The 's' stands for 'seed'. It's called "seeding" because you only do it once: once it's "planted", you have a stream from which you can call rand as many times as you need. Don't call srand at the beginning of the function that needs random numbers. Call it at the beginning of the program.

    Yes, it's a hack. But it's a hack with a very well-documented interface.

提交回复
热议问题