Why do I get the same result with rand() every time I compile and run?

后端 未结 4 761
陌清茗
陌清茗 2020-11-27 07:47

Whenever I run this code, I get a same result.

Program

#include

int main(int agrc, const char *argv[]) {
 int i = rand();
 printf(\"         


        
4条回答
  •  借酒劲吻你
    2020-11-27 08:33

    Just to add to Yacoby's answer - I was slightly surprised that it didn't default to a time-based seed, so I looked up the man page:

    If no seed value is provided, the rand() function is automatically seeded with a value of 1.

    So if you change your code to use seed(1) you should still see the same output - but seed(time()) will make it change each time.

提交回复
热议问题