How to generate a random int in C?

后端 未结 27 2136
故里飘歌
故里飘歌 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:52

    Well, STL is C++, not C, so I don't know what you want. If you want C, however, there is the rand() and srand() functions:

    int rand(void);
    
    void srand(unsigned seed);
    

    These are both part of ANSI C. There is also the random() function:

    long random(void);
    

    But as far as I can tell, random() is not standard ANSI C. A third-party library may not be a bad idea, but it all depends on how random of a number you really need to generate.

提交回复
热议问题