How to generate a random int in C?

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

    #include 
    #include 
    
    void main() 
    {
        int visited[100];
        int randValue, a, b, vindex = 0;
    
        randValue = (rand() % 100) + 1;
    
        while (vindex < 100) {
            for (b = 0; b < vindex; b++) {
                if (visited[b] == randValue) {
                    randValue = (rand() % 100) + 1;
                    b = 0;
                }
            }
    
            visited[vindex++] = randValue;
        }
    
        for (a = 0; a < 100; a++)
            printf("%d ", visited[a]);
    }
    

提交回复
热议问题