How to generate a random int in C?

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

    #include
    #include
    #include
    
    //generate number in range [min,max)
    int random(int min, int max){
        int number = min + rand() % (max - min);
        return number; 
    }
    
    //Driver code
    int main(){
        srand(time(NULL));
        for(int i = 1; i <= 10; i++){
            printf("%d\t", random(10, 100));
        }
        return 0;
    }
    

提交回复
热议问题