What Is The mt19937 Random Generator In Modern C++?
Random numbers are one of the most important parts of today’s modern programming technologies. They are used in mathematics, physics, in many engineering fields, and in programming such as generating random data for testing, random maps in levels, random trees on a planet – the list is endless. Since C++11, mt19937 (std::mt19937) is implemented as a random number generator. In this post, we explain what mt19937 is and how we can use it. What is a random number and a random number generator in C++? A random number is a number that is randomly chosen in a given range. It is impossible to predict future values based on past or present values and they are uniformly distributed over a defined interval or set. Mersenne prime is a prime number used in mathematics that is a number of the form Mn = 2n − 1 where the n is an integer. The Mersenne Twister is a pseudorandom number generator where the period length is chosen to be a Mersenne Prime. It was developed by Makoto Matsumoto in 1997. Since C++11, the Mersenne Twister mathematical number generator is implemented as a random generator number, it is defined in the header as a std::mersenne_twister_engine that is a random number engine based on Mersenne Twister algorithm. What is the std::mt19937 random number generator in modern C++? In C we use rand(), srand() and in C++ we use std::rand(), std::srand(). While they are added to to make modern C++ compatible, there are more useful and modern random number generators. These are std::mt19937 and std::mt19937_64. The std::mt1993 is a 32-bit Mersenne Twister by Matsumoto and Nishimura in 1998, and std::mt19937_64 is a 64-bit Mersenne Twister by Matsumoto and Nishimura in 2000. The std::mt19937 is a random number generator defined in the header in C++17 standard and beyond, producing 32-bit pseudo-random numbers by using the Mersenne Twister algorithm with a state size of 19937 bits. This is why it is called mt19937 and there is a 64-bit version called mt19937_64. Both are defined as an instantiation of the mersenne_twister_engine. Now let’s see their definitions. Since C++11, mt19937 is defined as below, typedef mersenne_twister_engine mt19937; Since C++11, mt19937_64 is defined as below, typedef mersenne_twister_engine mt19937_64; How can we use the random number generator std::mt19937 in modern C++? Simply we can generate modern random number as shown below. std::mt19937 rnd( std::time(nullptr) ); we can use it like so: unsigned int r = rnd(); if you want to generate a number in a range you can use modulus operator %.This is how can we generate random number between zero to n, i.e. 0 to 100. unsigned int r = rnd()%100; This is how can we generate random number between two numbers, i.e. 50 to 150, unsigned int r = 50 + rnd()%100; Is there a simple example to use std::mt19937 in modern C++? Here is a simple example to use std::mt19937. #include #include #include int main() { std::mt19937 rnd( std::time(nullptr) ); std::cout
