requirement ：
1） The computer generates a random number
2） Players guess numbers , If the guess is larger than the generated random number , The system will prompt “ Guess big ”; Similarly, I guess so , Until you guess right .

first , We need to know C Operation of generating random numbers in language . This and rand() function ,srand() function ,time_s() Function correlation .
rand() function ：
Function prototype ：
int rand(void); It returns an integer type , And included in the header file stdlib.h in . But when we write the following code ：
int main() { int num = 0; int i = 0; for (i = 0; i < 10; i++) { num = rand();
printf("%d ", num); } return 0; }
But we run many times at different times , Are the following results ：
therefore rand() Functions are not functions that generate random numbers , We can even predict its random sequence .

* Why is the number generated each time fixed ?

rand() The number each time the function generates is the same as the so-called " seed " of . use rand() Function to be used before srand() Function type " seed ". If not used srand() function , The system will default to 1, Causes the random number to be generated one at a time .
srand() function ：
Function prototype ： void srand(unsigned seed);//unsigned seed Unsigned seed
But when called srand() Function passed in a seed , In this way, the generated random number is still fixed ( Because the seed remains the same , Similar to the system default 1).

resolvent ： Seed system time , Because the seed is system time , Achieve different results ( Time is always changing ).

Time function time():
Using time function time() get SysTime （ Its return value time_t Value type must be ）.time() The parameter of is a time_t Address of type object , The time value is stored in this address .time(NULL) and time(0) Returns the system time , from 1970 year 1 month 1 day 0 spot 0 branch 0 Seconds , In seconds , Included in header file time.h in .
To sum up ：srand((unsigned)time(NULL));
From the top ：

thus , The task of generating random numbers is completed !

How to fix the range of random numbers ?
common method ： Random number modulus +1（ Like random numbers %100, Result in 0-99 between , plus 1 Then in 1-100 between ）

The code implementation is as follows ：
printf("***********************************\n"); printf("**** 1. Enter the game 0. Exit the game
****\n"); printf("***********************************\n"); } void game()
// Functions about game implementation { int guess = 0; int ret = rand() % 100 + 1;// generate 1-100 Random number of int count
= 1;// Count variables to guess the number of times printf(" The game begins , You have 10 Second chance , Please guess the number :>\n"); while (1) { scanf("%d", &
guess); if (guess < ret) { printf(" Guess it's small , You have %d Second chance \n",10-count); count++; } else if
(guess > ret) { printf(" Guess big , You have %d Second chance \n",10-count); count++; } else { printf(
" congratulations , Yes %d You guessed right \n",count); system("pause"); system("cls"); break; } if (11 ==
count)//count from 1 Add up , plus 10 second 1 yes 11 { printf(" The number of guesses is exhausted , Game failure \n"); system("pause"); system(
"cls"); break; } } } int main() { int input = 0; srand((unsigned)time(0)); do {