Several ways to get random numbers in ++
Basic concepts of random numbers
Random number: Within a certain range [a, z], the probability of each number appearing equally and the numerical sequence of the next number cannot be predicted.
Pseudo-random number generator (PRNG)
- Principle: It consists of a status register and an update function. The initial state is determined by the seed. The update state will generate the next state based on the current state and output a pseudo-random number.
- Seed: The initial value of the pseudo-random number generator, which determines the state at the beginning of the random number. Since the random number generates a random sequence based on the algorithm and the state at the beginning of the random number generator, the same seed generates the exact same random sequence.
2. Get random numbers in C++
2.1 Basic Process
Set random number seeds
Get random numbers (integral, floating point number)
2.2 Random number seed source
time()
Function
Get the current timestamp. In multi-threaded scenarios, due to the limited accuracy of the time function, the same seed may be generated and similar random sequences may be generated.
std::random_device
It is a nondeterministic source of random numbers that regains true random information from the operating system or hardware device.
2.3 Get random numbers
rand function in C cstdlib library
#include <ctime> #include <cstdlib> #include <iostream> int main() { // Set random number seeds std::srand(time(nullptr)); std::cout << "RAND_MAX" << RAND_MAX << std::endl; for (int i=0; i<10;++i) { // Get random numbers std::cout << "random value: " << rand() << std::endl; // Get the random number in the specified range by % std::cout << "random range 1 100: " << rand() % 100 << std::endl; } };
C++ random library (c++11)
#include <random> #include <iostream> int main() { //Specify seeds std::default_random_engine engine(std::random_device{}()); int count = 10; // Specify range: integer std::uniform_int_distribution<int> rand_int_generator(0, 100); for (int i=0; i<10; ++i) { std::cout << "int random value:" << rand_int_generator(engine) << std::endl; } // Specify range: floating point number std::uniform_real_distribution<double> rand_double_generator(0, 1); for (int i=0; i<10; ++i) { std::cout << "double random value:" << rand_double_generator(engine) << std::endl; } };
Randomness is highly requiredstd::mt19937
As an engine
#include <random> #include <iostream> int main() { //Specify seeds std::mt19937 engine(std::random_device{}()); int count = 10; // Specify range: integer std::uniform_int_distribution<int> rand_int_generator(0, 100); for (int i=0; i<10; ++i) { std::cout << "int random value:" << rand_int_generator(engine) << std::endl; } // Specify range: floating point number std::uniform_real_distribution<double> rand_double_generator(0, 1); for (int i=0; i<10; ++i) { std::cout << "double random value:" << rand_double_generator(engine) << std::endl; } };
3. Mersenne Twister (Matsett Rotation Algorithm)
3.1 Working principle
The Mersenne Twister algorithm maintains an internal state vector, which is usually of n w -bit words in length. For the most commonMT19937
Version, n=624 and w=32
initialization
First, initialize the internal state vector using a seed (can be any integer). Usually some processing is done on the seed, extending it to the length of the internal state vector
Status update
- The state vector is updated through a complex bit operation function, which is called the "twist" operation. exist
MT19937
In , it uses a series of shift, exclusive or sum operations to mix and update elements in the state vector - After multiple "twist" operations, the elements in the state vector will change in a complex way, ensuring the unpredictability of the next random number.
Random number extraction
Extract random numbers from the updated state vector. A function is usually used to map elements in a state vector to the desired output range. For example, for generating 32-bit random numbers, the elements in the state vector are used directly, while for generating random numbers less than 32-bit, the elements in the state vector are properly truncated or bit operations to generate the required random numbers
3.2 Features
1. Long cycle
Mersenne Twister's period is usually 2^19937-1, which is a huge number, making the generated random number sequence very long, and can avoid repeated sequence problems caused by short periods in practical applications.
2. High-dimensional uniform distribution
The generated random numbers have good uniform distribution characteristics in high-dimensional space, which is very important for situations where multiple random numbers are required for simulation or calculation.
3. High quality of randomness
Generating random numbers through complex bit operations and state transfer functions overcomes the disadvantages of some simple random number generators (such as linear congruent generators), such as the predictability and low quality of generated random number sequences.
This is the article about the common methods of obtaining random numbers in C++. For more related content for obtaining random numbers in C++, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!