How can I generate different random numbers for each player?

后端 未结 3 1383
轮回少年
轮回少年 2020-12-12 08:12

Both players get the same random number! ّI want each player to get a different number since they are throwing dice. here is the code:

#include 

        
3条回答
  •  -上瘾入骨i
    2020-12-12 08:40

    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().

提交回复
热议问题