How to generate a random int in C?

后端 未结 27 2103
故里飘歌
故里飘歌 2020-11-22 00:31

Is there a function to generate a random int number in C? Or will I have to use a third party library?

27条回答
  •  悲哀的现实
    2020-11-22 00:51

    This is hopefully a bit more random than just using srand(time(NULL)).

    #include 
    #include 
    #include 
    
    int main(int argc, char **argv)
    {
        srand((unsigned int)**main + (unsigned int)&argc + (unsigned int)time(NULL));
        srand(rand());
    
        for (int i = 0; i < 10; i++)
            printf("%d\n", rand());
    }
    

提交回复
热议问题