Same random numbers every time I run the program

前端 未结 4 968
日久生厌
日久生厌 2020-11-30 08:37

My random numbers that output, output in the same sequence every time I run my game. Why is this happening?

I have

#include  
         


        
4条回答
  •  隐瞒了意图╮
    2020-11-30 09:24

    You need to give the randum number generator a seed. This can be done by taking the current time, as this is hopefully some kind of random.

    #include 
    #include 
    using namespace std;
    
    int main()
    {
        int  r;
        srand(time(0));
        r = rand();
        return 0;
    } 
    

提交回复
热议问题