How can I generate different random numbers for each player?

女生的网名这么多〃 提交于 2019-11-28 14:52:43

srand ( time(NULL) ); is used to seed the pseudo-random number generator. time() having a granularity of 1 second, if you seed the PNRG every time you call the roll_a_dice() function, for all the calls made within the granularity period, rand() will end up returning the same random number.

Move the srand ( time(NULL) ); out of the roll_a_dice() function, call that only once in main().

You only need to seed once. Move srand to the top of main and it will work.

srand( int ); is used to initialize a seed. From there each time you call rand() you'll get a new random value. By calling srand() in roll_a_dice() you keep reseting the seed every time. Just move srand() at the start of your main().

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