How to generate random numbers + detailed analysis of the principle

Speaking of how to generate a random number, you may see such a piece of code after Baidu.

srand((unsigned int)time(NULL));
int ret = rand();

So how exactly is a random number generated? I believe that you who are good at exploring must want to know the principles of this, so let's not say much, let's get to the topic!
How a random number is generated:
When it comes to generating random numbers, we all know that we need to use a rand function, so how is this function used? We can open MSDN to see how this function is used
Insert picture description here
. The first sentence here says When the rand function returns an integer from 0 to RAND_MAX, what is the value of RAND_MAX? We can copy it to the compiler and select it, right-click and click the mouse to go to the definition, you can see the sentence
Insert picture description here
is actually The value of RAND_MAX is also 0x7fff, which is converted to decimal, which is 32767, so the rand function can randomly generate an integer from 0 to 32767. When you try it in the compiler, you will see
Insert picture description here
when you naively think you have succeeded When you can generate random numbers, you will find that when you run the code again that year, it will generate these random numbers, that is, the code generates random numbers the first time the code is run, but the same is generated the second time it is run random number.
At this time, we should remember that there is a second sentence in the description of the rand function: Before calling the rand function, we have to use the srand function to set the starting point for generating random numbers. Let's look up the srand function in MSDN:
Insert picture description here
we can see that the parameter of the srand function is an unsigned integer and has no return value. Then we can test it, just pass an unsigned integer to the srand function
Insert picture description here
But when we execute the program again, these random numbers are still the same. When we change the incoming srand function, we find that the random number given has changed:
Insert picture description here
so we only need to pass a and to the srand function every time the program is executed. The last time was a different number, but we just want to generate a random number, and now we need another random number. Isn't this an endless loop?
At this time, we thought that there is something on the computer that is changing all the time, and that is time. At this time, we need to introduce a concept, that is, timestamp.
Timestamp: The difference between the current time and the start time of the computer, in seconds.
The starting time of the computer: 1970-01-01 08:00:00
Insert picture description here
The timestamp is different every second, so we only need to pass the timestamp to the srand function, then we need to use the time function, because The return value of the time function is the timestamp.
Insert picture description here
Here we can see that the parameter of the time function is a pointer of time_t type, and the return value is of type time_t. We can also put the time_t in the compiler by right-clicking the mouse and clicking to go to the definition:
Insert picture description here
here we can see that actually time_t The int type is redefined by typedef (that is, it has an alias).
And we don’t need to pass in any pointer to the time function, so we can pass in a null pointer (NULL) to the time function, that is, time (NULL), but the parameter of the srand function is an unsigned int type, so if to pass time return value of the function srand function, then we need to return the time value of mandatory conversion will function unsigned int type, that is, (unsigned int) time (NULL)we write the final code is:
Insert picture description here
so , What you get every time you run the code is a real random number.
How to generate a random number with a specified number of digits:
Generate a two-digit random number:
Insert picture description here
We only need to take the remainder of the random number obtained by 90, then the number we get is the number 0-89, and then adding 10 is 10-99 The numbers are up.
Generate three random numbers: The
Insert picture description here
reason is the same as that of generating two random numbers. I will not elaborate.

As for the MSDN used in the article, if you don’t have one, you can search http://www.cplusplus.com. You can also view information about related functions.

Guess you like

Origin blog.csdn.net/chenlong_cxy/article/details/112862909