[C + +] random number and the usage of [C++11] / rand() and srand()

random number

C + + provides a set of functions to generate and use random numbers. A random number is a value obtained by randomly selecting from a set of possible values. All values in this group have the same chance of being selected.

Random numbers are commonly used in many different types of programs. Here are some examples:
Computer games usually use random numbers to simulate some random processes, such as rolling dice or dealing cards.
The simulation program uses random numbers to determine a series of subsequent operations or human and animal behaviors. You can create formulas that use random numbers to determine when a particular event occurs in your program.
The data analyzer may use random numbers to randomly select the data to be tested.
Computer security systems use random numbers to encrypt sensitive data.

We know that the random number function in C has only one rand(). It is very troublesome to generate random numbers and even random floating-point numbers in a certain interval.

The random library provided in C++11 solves this problem, which allows us to easily generate the required random values.

The following describes how to use the components in the random library to generate qualified random numbers.

The components in the random library are divided into two categories - random number engine class and random number distribution class.

Random number engine class is a random number generator that can run independently. It generates a certain type of random number with uniform probability, but it cannot specify the range, probability and other information of random number. Therefore, it is also known as the "original random number generator". Since the range of generating random numbers cannot be specified, it is usually not used alone.

The random number distribution class is a class that needs the support of the random number engine class to run, but it can generate qualified random numbers by using the random number engine according to the needs of users, such as random numbers of a certain interval and a certain distribution probability.

All random number engine classes support the following operations:

1. Use of random numbers in C + + 11 (recommended)

The commonly used random numbers are as follows:

default_random_engine: random non negative number (not recommended to be used alone).
uniform_int_distribution: a random nonnegative number in the specified range.
uniform_real_distribution: the random real number of the specified range.
bernoulli_distribution: Specifies the random Boolean value of the probability.
In fact, the random library is extremely functional, and the random number engine has more than default_random_engine is one, and the distribution class is far more than the above three. It can also carry out advanced random number functions such as Poisson distribution, normal distribution and sampling score. If you want to know more about these contents, please refer to other materials. I may have to wait a long time to fill in these contents.
Detailed reference:

1.1

1.2

1.3

1.4

2.1 usage of random functions rand() and srand() in C + + (old version)

The C + + library has a function called rand (), which returns a non negative integer every time it is called. To use the rand() function, you must include a header file in your program. The following is an example of its usage:

random_num = rand();

However, the number returned by this function is actually a pseudo-random number. This means that they have the performance and properties of random numbers, but they are not random in fact. They are actually generated by algorithms.

The algorithm requires a starting value, called seed, to generate numbers. If a seed is not given, it will produce the same digital stream each time it runs. The following procedure illustrates this:

#include <iostream>
#include <cstdlib>// Header file needed to use rand
using namespace std;
int main()
{
    // Generate and printthree random numbers
    cout << rand() << " ";
    cout << rand() << " ";
    cout << rand() << endl ;
    return 0;
}
Output result of the first operation:
41 18467 : 6334
 Output result of the second operation:
41 18467 6334

To get a different stream of random numbers each time you run the program, you must provide a seed for the random number generator to start. In C + +, this is done by calling the srand function.

Before rand is called, the srand function must be called first, and srand is called only once in the whole program.

#include <iostream>
#include <cstdlib> // Header file needed to use srand and rand
using namespace std;
int main()
{
    unsigned seed;  // Random generator seed
    // Get a nseed" value from the user
    cout << "Enter a seed value: ";
    cin >> seed;
    // Set the random generator seed before calling rand()
    srand(seed);
    //Now generate and print three random numbers
    cout << rand() << " ";
    cout << rand() << " ";
    cout << rand() << endl;
    return 0;
}
Results of the first operation:
Enter a seed value: 19
100 15331 - 209
 Results of the second operation:
Enter a seed value: 171
597 10689 28587

It can be seen from the output of the program that each time the program runs with different seeds, different random number streams will be generated. However, if you run the program again using 19 or 171 as a seed, you will get exactly the same number as the first time.

In line 12 of the program, use cin to obtain the value of the random number generator seed from the user's input. In fact, another common way to get seed values is to call the time function, which is part of the C + + standard library.

The time function returns the number of seconds elapsed since midnight on January 1, 1970, so it will provide a different seed value each time the program runs. The following program demonstrates the use of the time function. Note that you must pass it a parameter 0 when calling it. At the same time, the program contains a new header file ctime, which is necessary to use the time function.
The time function returns the number of seconds elapsed since midnight on January 1, 1970, so it will provide a different seed value each time the program runs. The following program demonstrates the use of the time function. Note that you must pass it a parameter 0 when calling it. At the same time, the program contains a new header file ctime, which is necessary to use the time function.

#include <iostream>
#include <cstdlib> // Header file needed to use srand and rand
#include <ctime> // Header file needed to use time
using namespace std;
int main()
{
    unsigned seed;  // Random generator seed
    // Use the time function to get a "seed" value for srand
    seed = time(0);
    srand(seed);
    // Now generate and print three random numbers
    cout << rand() << " " ;
    cout << rand() << " " ;
    cout << rand() << endl;
    return 0;
}

2.2 limit the range of random numbers

Sometimes programs need a specific range of random numbers. To limit the range of random numbers to integers between 1 and a maximum value max, you can use the following formula:

number = rand() % max + 1;

For example, to generate a random number of 1 ~ 6 to represent the number of dice, you can use the following statement:

dice = rand() % 6 + 1;

Added by errtu on Thu, 30 Dec 2021 22:51:34 +0200