Generation of random numbers ：
On the generation of random numbers , If you go to Baidu , You will see two pieces of code like this ：

srand((unsigned int)time(NULL));
int ret=rand();

So how are random numbers generated ? What are the applications in programming ? Next, let's take a look at the principle of its generation .
stay MSDN Or website www.plusplus.com You can see the function and usage of this function in ：

In the red box above RAND_MAX What is it , Here we can VS View its definition in

It can be seen that it is a 0x7fff（16 Base system ） Number of , Convert to 10 Hexadecimal is 32767, therefore rand Function can generate 0—32767 Random number between , The code example is as follows ：

Can see , When using a loop to generate 5 When a random number , It appears on the screen 5 A different number , But careful you will find , When the program is running , The screen is still the original 5 Digit number , The two numbers are exactly the same
, Why ? It was in the picture above rand The blue box in the definition of the function says that it is in use rand Function srand function .
Let's see srand Definition of function ：

srand The function sets the starting point of a random number , And its parameter is an unsigned integer and has no return value . Let's test the function again ：

We can see , When srand（） The parameters in are different , The generated numbers are also different , So only guarantee srand The parameters in are constantly changing to ensure that the generated numbers are different , To realize the generation of random numbers , So how can we ensure that the parameter is always a variable ?
This is what we thought of. There is something that changes all the time on the computer , That's time , Here is a concept , That's the timestamp .
time stamp ： The difference between the current time and the computer start time , The unit is seconds .（ Computer start time ：1970-01-01 08:00:00）

Each second has a different timestamp , So we just pass in the timestamp srand Function , Then we need to use time function , because time The return value of the function is the timestamp .

It can be seen that time The arguments to the function are time_t Type pointer , The return value is time_t type , Let's elaborate here time_t Definition of ：

We can see ,time_t namely int Type quilt typedef Redefined , Still essentially int type . And we don't need to time Pass in any pointer , Pass in null pointer （NULL） that will do
. Form
time（NULL）, however srand The arguments to the function are unsigned in t type , So we need to time The return value of the function is forcibly converted unsigned int
type , that is ( unsigned int )time( NULL). So the final code is ：
int main() { int n = 5; srand((unsigned int)time(NULL)); while (n) { int ret =
rand(); printf("%d\n", ret); n--; } return 0; }
In this way, random numbers will be generated in the true sense every time the code is run .
Let's use the function of generating random numbers to write a small game of guessing numbers .

Guess the steps of the number game ：
1. Randomly generate a 1-100 Number between
2. Guess the number
3. If you're right , Congratulations , game over
4. If you guess wrong , Will prompt you to guess big or small , Then continue to guess , Until you guess right
The code is implemented as follows ：
#include<stdio.h> #include<time.h> void menu() { printf(
"**********************\n"); printf("******* 1.play *******\n"); printf(
"******* 2.exit *******\n"); printf("**********************\n"); } void game() {
int ret = rand() % 100 + 1;// Ingenious use % operation , obtain 1-100 Number between int guss = 0; while (1) {
printf(" Please guess the number ：\n"); scanf("%d", &guss); if (ret < guss) { printf(" Guess big \n"); }
else if (ret > guss) { printf(" Guess it's small \n"); } else { printf(" congratulations , You guessed right \n!"); break; }
} } int main() { int input = 0; srand((unsigned int)time(NULL));
// Put it in the main function to ensure the number of guesses in each game do // It won't change as the game goes on . { menu();// Menu function printf(" Please enter an option ："); scanf(
"%d", &input); switch (input) { case 1: printf(" The game begins \n"); game();// Game function break;
case 0: printf(" game over \n"); break; default: printf(" Input error , Please re-enter \n"); break; } }
while (input); return 0; }
Operation effect

Tips ：
How to generate random numbers within a specified range ? Above code
generate 1-100 Random number between , Can be written as : int ret = rand() % 100 + 1
Generate a two bit random number , Can be written as ：int ret=rand()%90+10
Generate a three bit random number ：int ret=rand()%900+100

There are other types of random numbers , Don't elaborate too much .

Technology
Daily Recommendation