An article takes you to understand random numbers

Posted by itsureboy on Thu, 23 Dec 2021 21:22:30 +0100

As the basic language of Java, c + + and other languages, c language inevitably has many disadvantages, such as too complex operation of memory, trouble in calling random numbers and so on.

Here we help you sort out the generation and calling of the following random numbers

·rand function and its associated srand function

              ·srand(); Principle of

·Reference to time() function

·Combination of srand and time

·Range control of random numbers

catalogue

·rand function and its associated srand function

              ·srand(); Principle of

·Reference to time() function

·Combination of srand and time

·Range control of random numbers

·rand function and its associated srand function

The rand function is used to randomly generate a number, which is said to be random, but it is essentially based on the srand function. If we don't use the srand function and only use the rand function to generate numbers, a string of regular integers will be generated, because the srand function is a constant by default (it's easy to understand here, we remember it as 1);

When entering the rand function, the number generated by each srand generates a series of algorithm calculations to generate the number in the Rand.

This exponential multiplication method can improve access speed and reduce memory usage.

(soul painter warning)

The above is the number generated by srand (recorded as A), and the following is A string of numbers generated based on A;

·srand(); Principle of

The Srand function serves the rand function to provide a constant for the rand function to stimulate it to generate characters

The usage method is: srand(); Inside is a number, which excites rand to generate a number, and the generated number then excites rand to generate characters.

· reference to time() function

As we mentioned earlier, srand needs a number to stimulate him. This number must be completely random. Here we use the time timestamp;

Time stamp refers to the current system time and the birth time of the computer 1971.1 The distance difference between 1 is an integer in seconds, which is different from time to time

Time(null); Indicates that a difference value is returned;

·Combination of srand and time

Srand((unsigned int)time(Null));

According to the above, a number is generated for srand through time, that is, a string of numbers is generated for rand

 

(soul painter again)

·Range control of random numbers

After generating the number again, we have to find a way to control his range

We just used the% module method, A%B

Taking modulus is the remainder, that is, take n b from this A number, and the remaining numbers are less than B as to. Therefore, using this feature, we can control the range of rand

Int ret =rand()%100; // The number generated by the control Rand is 0 to 99 (can't take away 100);

·Code sorting

#include <stdio.h>
#include <stdlib. h> / / header file of rand function
#include <time. h> / / time header file
Int main()
{
srand((unsigned int)time(null));
Int red = rand()%100; //Control 0 ~ 99
return 0;
}

·Project practice

Guess numbers games

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu();
void playgame();
int main()
{
	int sc = 0;
	int flag = 0;
	srand((unsigned int)time(NULL));
	do 
	{
		menu();
		scanf_s("%d", &sc);
		switch (sc)
		{
		case 1:
			printf("The game begins\n");
			playgame();
			break;
		case 2:
			flag = 1;
			break;
		default:
			printf("Input error, please re-enter\n");
		}
		if (flag)
		{
			printf("End the game,Exiting");
			break;
		}
		system("PAUSE");//System PAUSE
		system("CLS");//Clear screen
		system("PAUSE");//System PAUSE
	} while (1);
	return 0;
}
void menu()
{
	printf("---------------------\n");
	printf("******1,Play  ******\n");
	printf("******2,Exit  ******\n");
	printf("---------------------\n");
}
void playgame()
{
	int red = rand() % 100 + 1; //Generate 1 ~ 100
	int n = 0;
	while (1)
	{
		printf("Please enter the number you guessed>\n");
		scanf_s("%d", &n);
		if (n < red)
			printf("Guess it's small\n");
		else if (n > red)
			printf("Guess big\n");
		else if (n == red)
			break;
	}
	printf("Congratulations on your guess,game over\n");
	
}

Thank you! Pay attention to jiaran and satisfy your cravings!  

 

Topics: C C# Algorithm