Header file <cstdlib> has an important function of rand (), which can be used as a random number generator.
Now I want to generate a random number, I use the following program:
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
cout << rand () << Endl;
return 0;
}
Here's the problem, though we have a random number, but no matter how many times I run the program (recompile, run, same as 41), all produce a certain number. That is, 41, as follows:
This is why.
The answer is to get a random number, we need to enter a seed (seed) for this random number generator (random numbers generator).
Now let's call the rand () function 25 times in a function to produce 25 random numbers:
This is also the case, whether it is to run the program many times, or compile, run, the result is still generated 25 random numbers exactly the same as the above random number. This is still not the reason for seed.
In an example, simulate the dice. Randomly appearing data are: 1, 2, 3, 4, 5, 6, now we programmed to generate this range of numbers, cast 25 times, the program is as follows:
#include <iostream>
#include <random>
using namespace std;
int main ()
{
for (int i = 0; i < i++) {
cout << 1+ (rand ()% 6) << Endl;//must add 1, otherwise generate 0
> return
0;
}
The results of the operation are as follows:
It is not difficult to generalize, we can produce random numbers of integers of any range.
But the problem is that because the random numbers of the above programs do not have seed, all the random numbers produced are the same regardless of how many times we run the program.
Ask what.
Because no computer can produce completely random random numbers. After all, the computer is not human, the computer must follow certain algorithm (algorithm), certain instructions to carry out the order. This means that computers cannot produce completely random data. But the computer through a certain complex algorithm, we can make the data they produce appears to be random.
C + + produces seed random number that can be used by random numbers. The commonly used function is Srand () (the function has an argument that gives us any random number to enter):
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
srand (6);
for (int i = 0; i < i++) {
cout << 1+ (rand ()% 6) << Endl;
}
return 0;
}
The resulting results are as follows:
It's obviously different. But if we don't change the parameters in the Srand (6) function, as long as we run, or recompile, we still have the same result (same as the previous question). But as soon as we change the parameters in Srand (), recompile, run, there are different results. For example, if we change srand (6) to Srand (10), it changes:
Now we have the ultimate solution to all of the above problems:
This solution needs to include a header file called <ctime>. This header file allows us to get the computer's clock. As follows:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
srand (time (0));
for (int i = 0; i < i++) {
cout << 1+ (rand ()% 6) << Endl;
}
return 0;
}
Compile run:
Run again:
This means that as soon as it is rerun, it will be again generated and pseudo-random numbers. This is exactly what we want to achieve.
Now we explain why:
We know that we can modify our algorithm by Srand (). But if we pass a definite number to srand (), the result of the algorithm is also a definite number. No matter how many times we run, the results are the same.
But we want Srand to pass a parameter of time (0), and the return value of the time (0) function changes every second. Randomly, every time we run a function, the results seem random.