Same random numbers every time I run the program

前端 未结 4 967
日久生厌
日久生厌 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:12

    Pseudorandom number generators take a starting number, or seed, and then generate the next number in the sequence from this. That's why they're called pseudorandom, because if they always use the same starting value, they will generate the same sequence of numbers like the C standard lib generator does. This can be fixed by giving the generator a starting value that will change the next time the program is run like the current time.

    Anyway, the code you're looking for like others have said is:

    srand(time(0)); //Seed the generator, give it a starting value
    

提交回复
热议问题