Standard library <cstdlib> functions for random numbers in C++: rand() and srand()

The so-called "pseudo-random number" does not refer to a false random number, but the "pseudo" here means regularity. In fact, an absolute random number is just a random number in an ideal state, and a computer can only generate a relative random number, that is, a pseudo-random number. Computer-generated pseudorandom numbers are both random and regular — some obey certain rules, and some don't. For example, "there are no two leaves with the same shape in the world", which is exactly the characteristic of things - regularity; but the leaves of every tree have similar shapes, which is the commonality of things - regularity. From this perspective, we can accept the fact that computers can only generate pseudo-random numbers rather than absolute random numbers.

The standard library <cstdlib> in C++ (included in <iostream>) provides two functions to help generate pseudorandom numbers: rand() and srand().
Function 1: int rand(void);
starting from the specified seed in srand(seed), return a random integer in the range [seed, RAND_MAX(0x7fff))
Function 2: void srand(unsigned seed);
parameter seed is The random seed for rand(), which is the starting value used to initialize rand().

The system will automatically call srand() before calling rand(). If the user has called srand() before rand() and specifies a value for the parameter seed, then rand() will use the seed value as a pseudo-random number. If the user has not called srand() before rand(), then rand() will automatically call srand(1), that is, the system defaults to 1 as the initial value of the pseudo-random number.

It can be seen from the above that if you want rand() to generate different values ​​every time the program runs, you must specify a variable value for the parameter seed in srand(seed), which must be changed every time the program runs. Different (such as the elapsed time so far); if we specify a fixed value for seed, then every time the program runs, the random number generated by rand() will be the same, but the value is [seed, RAND_MAX (0x7fff)) a randomly taken value in the range.

To illustrate with a few examples, suppose we want to obtain a random number between 0 and 6 (excluding 6 itself):
Program 1 (the value of seed is not specified):
for(int i=0; i<10; i++)
{
    ran_num=rand()%6;
    cout<<ran_num<<“ ”;
}
Each time the program is run, it will output: 5 5 4 4 5 4 0 0 4 2

Program 2 (specify seed as 1):
srand(1);
for(int i=0; i<10; i++)
{
    ran_num=rand()%6;
    cout<<ran_num<<" ";
}
every time the program is run Both will output: 5 5 4 4 5 4 0 0 4 2, which is exactly the same as the result of program one.

Program 3 (specify the value of seed as 6):
srand(6);
for(int i=0; i<10; i++)
{
    ran_num=rand()%6;
    cout<<ran_num<<“ ”;
}
every time Running program three will output: 4 1 5 1 4 3 4 4 2 2, although the value is different from program two, the result is the same every time it is run.

Program 4 (the specified seed value is the elapsed time of the current system, in seconds (time_t time(0))):
#include<ctime>
    ……
srand((unsigned)time(0));
for(int i= 0; i<10; i++)
{
    ran_num=rand()%6;
    cout<<ran_num<<“ ”;
}
When running program 4, the first output: 0 1 5 4 5 0 2 3 4 2, the first Secondary output: 3 2 3 0 3 5 5 2 2 3, ... The result of each run is different, because the moment when the program is started is different each time.

About time_t time(0)
time_t is defined as a long integer, which will return the elapsed time in seconds from January 1, 1970 0:00:00 seconds. For example, if you output cout<<time(0), you will get a value of about 1169174701, which is about 37 (years) * 365 (days) * 24 (hours) * 3600 (seconds) (excluding months and days).

Regarding ran_num=rand()%6
, it is necessary to modulo the return value of rand() with 6, so as to ensure that the purpose random number falls between [0, 6), otherwise it is likely to get a very huge value (RAND_MAX Typically 32767). A general formula is: To get a random integer between [a, b), use (rand()%(ba)) + a, the result contains a but not b

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324844666&siteId=291194637