C + + uses <cstdlib> header file to generate a random number generator

Source: Internet
Author: User

Header file <cstdlib> has an important function 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 <ctsdlib><span style= "font-family:arial, Helvetica, Sans-serif;"    >using namespace Std;</span>int Main () {cout << rand () << Endl; return 0;}

The problem comes, although we produce a random number, but no matter how many times I run the above program (recompile, run, also 41), it produces a definite number. That is, 41, as follows:

What is this for?

The answer is that to get a random number, we need to enter a seed (seed) for this random number generator (generator).

Now let's call the rand () function in the function 25 times to generate 25 random numbers:

The same is true, no matter how many times the program is run, or if it is compiled and run again, the result is 25 random numbers that are exactly the same as the random numbers above. This is still the reason for not having seed.

In an example, simulate the dice. The randomly occurring 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 be added 1, otherwise 0    }    return 0 will be generated;}

The results of the operation are as follows:

Not difficult to generalize, we can produce random numbers of arbitrary range integers.

But the problem is that since the random number of several programs above does not have seed, all the random numbers that are generated are the same regardless of how many times we run the program.

Ask what?

Because no computer can produce a completely random random number. Computer is not a person after all, the computer must follow certain algorithm (algorithm), certain instructions to execute the command. This means that computers cannot produce completely random data. But the computer through a certain complex algorithm, we can make its generated data appears to be random.

C + + produces a seed random number that can be used by the stochastic. The usual function is Srand () (the function has a parameter that allows us to give in any random number):

#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:

Obviously different. But if we do not change the parameters in the Srand (6) function, as long as we run, or recompile, the same result will still appear (as in the above 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 will be changed:

Now we give the ultimate solution to all 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 re-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 (), then the result of the algorithm is also a definite number. No matter how many times we run, the result is 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 so, every time we run a function, the result looks random.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.