preface
The system generates a number between [1100], and the user inputs an integer at will. If the number entered by the user is smaller than the number generated by the system, the prompt "guess lower". If the number entered by the user is larger than the number generated by the system, the prompt guesses higher. If the number is the same, the prompt guesses right.
1, Mr. is a random integer of [1100]
The standard library of C language already contains many ready-made functions. rand can generate a random integer, and you can refer to cplusplus documents. Enter www.cplusplus.com in the browser com. cstdlib, which is written in C + + style, stdlib H is written in C language style. Stdlib can also be used in C + + h. If there are no function parameters in C language, you can write () or void directly, but the return value can not be ignored. Void can also be ignored in some old compilers, such as VC6. If the return value is omitted, it means that the return value type is int.
As can be seen from the above article, the random integer we want to generate [1100] is rand()%100+1
Note: Here's a reminder: the rand function should be used with caution in future work. Multithreading is not safe. In actual development, if the requirements for random numbers are not strict, there can be many schemes to replace Rand. For example, the address of a variable can be used to replace Rand.
2, Prompt the user for interaction
Add a menu to interact with users. The code is as follows:
int menu() { printf("*********************\n"); printf("Welcome to the number guessing game!\n"); printf("1.Start the game\n"); printf("2.Exit the game\n"); printf("Please enter your choice\n"); printf("*********************\n"); int choice = 0; scanf("%d", &choice); return choice; }
3, Pseudorandom and true random
Pseudo random number is a random value obtained by a certain algorithm, which is not really random.
True random number is accompanied by physical experiments, such as coin tossing, dice rolling, noise of electronic components, nuclear fission, etc. his results accord with three characteristics. Is a real random number.
When we execute the program by rand, we will find that the random number is the same every time, which is pseudo-random. So how to generate real random numbers? We can use srand. Just make the random seeds set different every time the program is executed, and we will think of time. Time is always changing. Time in the computer is represented by an integer, called a timestamp, which is very important. The time stamp takes 0:00:00:00 on January 1, 1970 as the reference time to calculate the difference between the current time and the reference time.
time(0) to get the timestamp, you need to include the header file time h. The return type of the time function is called time-t. time is essentially an unsigned long. Therefore, it is necessary to cast the time-t result to unsigned int. (this may cause data loss, so that the compiler does not have to prompt this warning).
srand((unsigned int)time(0));
4, Overall program implementation
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<time.h> int menu() { printf("*********************\n"); printf("Welcome to the number guessing game!\n"); printf("1.Start the game\n"); printf("2.Exit the game\n"); printf("Please enter your choice\n"); printf("*********************\n"); int choice = 0; scanf("%d", &choice); return choice; } void game() { printf("Start a number guessing game\n"); int toGuess = rand() % 100 + 1; while (1) { int inputNum = 0; printf("Please guess a 1-100 Integer of\n"); scanf("%d", &inputNum); if (inputNum > toGuess) { printf("Guess high!\n"); } else if (inputNum < toGuess) { printf("Guess low!\n"); } else { printf("You guessed right!\n"); break; } } } int main() { srand((unsigned int)time(0)); int choice = menu(); while (1) { if (choice == 1) { game(); break; } else if (choice == 0) { printf("goodbye!\n"); break; } else { printf("Your input is incorrect!\n"); } } system("pause"); return 0; } }