rand() not generating random numbers after modulo operation

后端 未结 8 2016
一向
一向 2020-12-07 04:43

I\'m taking a C refresher and took on a board game as an exercise. The board game is \"Game of the Generals\" and is pretty much like chess as it uses pieces with ranks on a

相关标签:
8条回答
  • 2020-12-07 05:10

    Make sure to initialize / seed the random number generator:

    #include <time.h>
    
    srand(time(NULL));
    
    0 讨论(0)
  • 2020-12-07 05:10

    If you don't seed the pseudo random number generator, it is like if you called in the start of your program:

    srand(1);
    

    You can call srand with an argument that depends on the time so that two successive calls of your program would generate different numbers:

    srand(time(NULL));
    
    0 讨论(0)
提交回复
热议问题