我该怎么做才能每次都使整数随机

What can i do to make integer random everytime

我创建了一个小猜谜游戏,您需要在 3 次尝试中猜出一个数字,否则您将失败。现在的问题是每次我 运行 程序时,我该怎么做才能使整数随机。

这是一个代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int sN = 4;
    int g;
    int gC = 0;
    int gL = 3;
    int ofG = 0;

    while(g != sN && ofG == 0)
    {
        if(gC < gL)
        {
            printf("Guess a number[1-10]: ");
            scanf("%d", &g);
            gC++;
        }
        else
        {
            ofG = 1;
        }
    }
    if(ofG == 1)
    {
        printf("You Failed! [Out of guesses]\n");
        printf("Secret number was (%d)\n", sN);
    }
    else
    {
        printf("Congratulations! You win!");
    }

  return 0;
}

告诉我我该怎么做,并解释你做了什么,如果可以的话 command/function 做了什么,因为我是新手,我不太了解事情。我想我需要使用

srand();

但我以前从未使用过它,因此将不胜感激。

#include <time.h>
#include <stdlib.h>

srand(time(NULL));   // Initialization, should only be called once.

/* random int between 0 and 9 */
int r = rand() % 10;