srand function is returning same values

牧云@^-^@ 提交于 2019-11-26 23:12:52

srand() is a function that sets the seed for the rand() function. What you are doing here is setting the seed to the current time before every rand() you call, which, if called fast enough, will get you the same value (since it will reset to the same seed, which if fast enough will be the same time value).

What you'll want to do is call srand() once, when the program starts (at the start of your main() function)

Then call rand() every time you want a random number, like you are doing currently, but without calling srand() every time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!