Is it good idea to pass uninitialized variable to srand?

前端 未结 5 2050
醉梦人生
醉梦人生 2021-01-20 02:19

Is it good idea to pass uninitialized variable to srand instead of result of time(NULL)?
It is one #include and one function call

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-20 02:39

    In the first case there are two possibilities:

    • seed is a random variable (less possible)
    • seed is constant on every run (more possible)

    time(NULL) returns the time of the system which is 99.99% different every time you run the code.

    Nothing is perfect random, but using time(NULL) gives you a "more random" number then if you would use the first approach.

    You should check function's usage http://www.cplusplus.com/reference/cstdlib/srand/

提交回复
热议问题