How does the random number generator work in C?

后端 未结 6 854
终归单人心
终归单人心 2021-01-23 06:29

I\'m trying to generate random number between 0 and 40(inclusive). So the code I Implemented is this-

 y=rand()%41;

However everytime I click c

6条回答
  •  南笙
    南笙 (楼主)
    2021-01-23 07:04

    Reason is that rand() is using the same seeding everytime you run. You have to seed it yourself. srand ( time(NULL) );` is usually used to initialize random seed.

提交回复
热议问题