Same random numbers every time I run the program

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

    You need to seed your random number generator:

    Try putting this at the beginning of the program:

    srand ( time(NULL) );
    

    Note that you will need to #include .

    The idea here is to seed the RNG with a different number each time you launch the program. By using time as the seed, you get a different number each time you launch the program.

提交回复
热议问题