且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为每个玩家生成不同的随机数?

更新时间:2023-02-06 08:46:29

srand ( time(NULL) );用于植入伪随机数生成器. time()的粒度为1秒,如果您每次调用roll_a_dice()函数时都为PNRG设置种子,那么对于在粒度周期内进行的所有调用,rand()最终将返回相同的 random 数字.

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.

srand ( time(NULL) );roll_a_dice()函数中移出,仅在main()中调用一次.

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