How does the random number generator work in C?

后端 未结 6 899
终归单人心
终归单人心 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 06:52

    It is generated by an argument passed seed. To generate different numbers add this before calling the rand() function:

    srand (time(NULL));
    

    This generates a new random seed.

    You should have this library: #include And if you still have an error use this one as well: #include

提交回复
热议问题