Brief introduction of guessing number game and pseudo-random number in C language

Brief introduction of guessing number game and pseudo-random number in C language

Introduction

  • The definition of number
    guessing game The number guessing game is a mini program that automatically generates a number within a given range, and the user completes the successful input of random numbers according to the system prompts (guess high, guesses low).
  • The realization of the number
    guessing game The number guessing game uses the C languageBranch loop knowledgeA small program written with a simple user interface and a combination of simple functions.
  • The logical structure of the number guessing game
    1. The system generates random numbers according to the given conditions;
    2. The user inputs a number in a given range;
    3. The system further runs the program according to the generated random number and the number input by the user.

1. Design of user interface

In order to have oneThe complete "game" experience, We will print a small game interface:
in the main function main(), the input herechoiceCome into different stages.

int menu() {
    
    
	printf("=============================\n");
	printf("==========1. 开始游戏========\n");
	printf("==========0. 退出游戏========\n");
	printf("=============================\n");
	printf(" 请输入您的选择: ");
	int choice = 0;
	scanf("%d", &choice);
	return choice;
}

2. Game program design

The program will generate arandom number, We use the library functionrand functionTo achieve this function (note that the random number here isPseudo random number)。

Pseudo-random numbers are calculated by a deterministic algorithm from a sequence of uniformly distributed random numbers from [0,1]. It is not truly random, but has statistical characteristics similar to random numbers, such as uniformity and independence. When calculating pseudo-random numbers, if the initial value (seed) Is unchanged, then the number sequence of the pseudo-random number is also unchanged. (Baidu Encyclopedia)
The value of the random number is related to the seed used. In order to achieve a real random number, we can add a method to replace the seed in the program. (Used belowTimestampMethod of making seeds)

Insert picture description hereGenerate random number generates random numbers;
int rand (void); is a reference method;
the header file in c is:

#include<stdlib.h>

The figure below shows the functionMethods of calling different random number ranges:
Insert picture description hereWe can follow his example to specify the range of random numbers. I gave an example of 1-100 here, you can specify other ranges yourself.

void game() {
    
    
	int toGuess = rand() % 100 + 1;// 1. 程序自动生成一个 1-100 的随机整数. 
	while (1) {
    
    
		printf("请输入要猜的数字: ");
		int input = 0;
		scanf("%d", &input);
		// 3. 比较用户输入的数字和系统生成的数字之间的关系, 并给用户提示. 
		if (input < toGuess) {
    
    
			printf("低了!\n");
		}
		else if (input > toGuess) {
    
    
			printf("高了!\n");
		}
		else {
    
    
			printf("猜对了!\n");
			break;
		}
	}
}

3. Implementation of the main function

TimestampIt refers to the total number of seconds since 00:00:00, January 1, 1970, Greenwich Mean Time (08:00, 01:01, 1970, Beijing Time) to the present. In layman's terms, a time stamp is a complete verifiable data that can indicate that a piece of data has existed at a specific point in time. We can use the characteristics of timestampImplement random seed input.
The specific method is:

srand(time(0));

The header file is:

#include<time.h>

The main function is as follows:

int main()
{
    
    
	srand(time(0));
	//将现在的时间戳作为随机数种子
	while (1) {
    
    
		int choice = menu();//给用户一个交互的菜单
		if (choice == 1) {
    
    //用户开始游戏
			game();
		} else if (choice == 0) {
    
    
			printf("程序退出, 再见!\n");
			break;
		} else {
    
    
			// 写程序一定要考虑到用户的非法输入的情况. 
			printf("您的输入有误, 请重新输入!\n");
		}
	}
	system("pause");
	return 0;
}

Insert picture description hereThe picture shows an output result.
You can copy my code directly to have a try. QAQ
remember to add header files:

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

Guess you like

Origin blog.csdn.net/zhaocx111222333/article/details/109220501