How to generate a random int in C?

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

    #include 
    #include 
    
    int random(int range);
    
    int main(void)
    {
        printf("%d", random(10));
        return 0;
    }
    
    int random(int range)
    {
        struct time t;
        int r;
    
        gettime(&t);
        r = t.ti_sec % range;
        return r;
    }
    

提交回复
热议问题