random number
The function generated by random number is rand(), which is in the standard library, so we need to add its header file < stdlib h>
Error demonstration!!!
#include <stdio.h> #include <stdlib.h> int main() { int ret; ret=rand(); printf("%d\n",ret); return 0; }
In this way, the simplest random number is generated, but this method has a fatal disadvantage. If we only execute it once, the result will be the same every time. If we execute it five times in a row, the result will be the same five times. Therefore, the above writing is problematic!!!
The rand() function does not simply generate random numbers. Before calling it, you need to use the srand() function to set the random number generator.
This time, let's use the srand() function first
#include <stdio.h> #include <stdlib.h> int main() { int ret; srand(1); ret=rand(); printf("%d\n",ret); return 0; }
However, when we go to the test, we find that it is still a fixed value. Only after the parameters of the input srand() function change, the random number will change. Therefore, there is still a problem in this way. The problem lies in the srand() function, which needs to input a random number as the starting point. Therefore, we introduce a concept called timestamp.
time stamp
Concept of timestamp:
Current computer time - computer start time (January 1, 1970, 0:0:0) = () seconds.
Therefore, when our time is changing, our timestamp will change from time to time.
Therefore, as long as we get the timestamp, we can set the starting point of random number! We need to call the time function!
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int ret=0; srand((unsigned int)time(NULL)); ret = rand(); printf("%d\n",ret); }
But it's not easy for us to observe. We write a simple test program to see if our program is correct. Let's write a simple test program, let random numbers generate numbers, and let's test.
#include <stdio.h> #include <stdlib.h> #include <time.h> void randomtest() { int ret = 0; ret = rand() % 100 + 1; printf("The generated random number is%d\n", ret); } int main() { int input = 0; srand((unsigned int)time(NULL)); printf("**************************\n"); printf("***** 1.go 0.back *****\n"); printf("**************************\n"); do { printf("Please select:>"); scanf("%d", &input); switch (input) { case 1: randomtest(); break; case 0: break; default: printf("Wrong selection, please select again!\n"); break; } } while (input); return 0; }
In this way, we can generate random numbers arbitrarily, and the random numbers generated each time are different, so we can master the generation and use of random numbers.
Another important thing to note here is that we put srand() in the main function, which means that our srand() function only needs to be set once, without repeated settings and frequent calls.
In order to deepen the impression, use random numbers to make a simple game.
Figure guessing game
#include <stdio.h> #include <stdlib.h> #include <time.h> void menu() { printf("****************************\n"); printf("***** 1.play 0.exit *****\n"); printf("****************************\n"); } void game() { int ret=0; int input=0; ret = rand()%100+1; printf("Guess the number\n"); while(1) { scanf("%d", &input); if(input>ret) printf("Guess big!\n"); else if(input<ret) printf("Guess it's small!\n"); else { printf("Congratulations, you guessed right!\n"); break; } } } int main() { int input=0; srand((unsigned int)time(NULL)); do { menu(); scanf("%d",&input); switch(input) { case 1: game(); break; case 0: break; default: printf("Wrong selection, please select again!\n"); break; } }while(input); return 0; }
For the game of guessing numbers, we set the random number between 1-100. If you want to set the range of random numbers, you can modify it in game()!