Re-recognize and learn random numbers (rand()) and time functions (time(NULL)) through number guessing games

       Hello everyone! Today, let’s introduce a knowledge point through a small game: generating random numbers. All concepts introduced below have corresponding links for matching. Please click on the link to view the details.

Generate random numbers:

       First, let’s introduce the random function rand() : https://legacy.cplusplus.com/reference/cstdlib/rand/?kw=rand . The return value is a pseudo-random number from 0 to RAND_MAX (32767) . Before using the rand function, you must first use the srand function to set the starting point for random number generation . (Some friends may not understand this sentence, but continue reading below.)

       Let’s introduce the srand() function : https://legacy.cplusplus.com/reference/cstdlib/srand/?kw=srand . The parameters of the function are: seed generation of pseudo-random numbers .

       Both of the above functions mentioned: pseudo-random numbers , so what exactly are pseudo-random numbers? Come and explore with the editor below.

Pseudo random numbers:

       It is impossible to generate completely random numbers in a computer, what is generated is a pseudo-random number. For example, we are now writing a program to generate random numbers, but in theory the random numbers generated are the same every time we run the program . code show as below:

int main()
{
	int i = 0;
	while (i < 3)
	{
		int ret = rand();
		printf("%d\n", ret);
		i++;
	}
	return 0;
}

 

       Therefore, just using a rand() function cannot generate a random number. In order to return a different value every time it is called , there is the concept of seed .

seed:

       (The seed is not the seed of bt.) As mentioned before, the parameter of the srand function is the seed. Its function is to make the parameters different every time the function is called, so that the return value is different.

       Let’s do the hand-shredding code: (We need to get used to learning and adapting to hand-shredding some library functions so that we can use them better)

Use C language to implement the rand() function and srand() function:

Let’s look at the rand() function first: This next data can be multiplied or divided at will, and I don’t think it matters.

static unsigned long int next = 1;  //将数据放在静态区中
unsigned int randX() 
{
	next = next * 1103515245 + 12345;
	return (unsigned int)(next / 65536) % 32768;
}

Let’s look at the srand() function:

void srandX(unsigned int seed)
{
	next = seed;
}

       After talking about the above two functions, let's talk about how to actually print out random numbers on the computer. In the previous section, every time we change the seed of the srand() function, we will find that the generated random numbers continue to change . And we know that a changing srand value can correspond to different random values, and in computers, time is the value that is always changing. For each changed time, we have a unique number that can represent it. This number is called a timestamp. https://tool.lu/timestamp/

time function:

       Let’s briefly introduce this function. The details are at: https://legacy.cplusplus.com/reference/ctime/time/?kw=time . This function can return a timestamp (the time of the current machine and the starting time of the computer. difference).

The parameter        of the time function is a pointer . If you don't want to use it, just write it as NULL . Its return type is: time_t, so cast type conversion is required when using the time function.

srand((unsigned int)time(NULL))

       Thank you for watching. See you in the next article. Criticisms and corrections are welcome.

Guess you like

Origin blog.csdn.net/2301_77868664/article/details/131793519