rand() return the same number [duplicate]

最后都变了- 提交于 2019-12-01 23:26:21

It is because you are reseeding it every time, and as your program probably runs very quickly, your seed value (time()) is the same for each call as it's smallest increment is 1 second.

Try moving srand() into main() instead and call it once

Repeatedly calling srand ruins the statistical properties of the generator.

Call it no more than once.

(The observed behaviour is caused by time(NULL) returning the same value in every iteration of the loop.)

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