Std::mt19937 class in C++

In this article, you will learn about the std::mt19937 class in C++ with its syntax, parameters, and examples.

In C, we use the functions such as rand() and srand(), while in C++, we use std::rand() and std::srand(). Numerous more advanced random number generators are available to align with modern C++ standards. There are the 32-bit Mersenne Twister std::mt19937 and the 64-bit Mersenne Twister std::mt19937_64, developed by Matsumoto and Nishimura in 1998 and 2000.

The standard::MT19937 (as in C++11) class is defined in a random header file and is a very efficient pseudo-random number generator. It uses the well-known and widely used Mersenne Twister method to generate 32-bit pseudorandom numbers. The std::mersenne_twister_engine class is essentially a subset of the std::mt19937 class.

The std::mt19937 random number generator is shown in the title of the C++17 standard and later versions. Use the Mersenne Twister algorithm to generate a pseudo-random number with a state size of 19937 bits and a bit length of 32. For this reason, it is called mt19937, the name of the mt19937_64 64-bit version. Both are shown as mersenne_twister_engine examples.

Syntax:

Here, mt1 is an instance of the mt19937 class and the seed value required to obtain the entire sequence.

Importance Of The Name mt19937

219937 - 1, or mt19937 has long had a computer program called the Mersenne twister that produces a 32-bit sequence of numbers that does not repeat until the number 219937 - 1 is produced.

Comparison between rand() & srand() and mt19937:

Std::mt19937 does two things.

  • Instantiating a std::mt19937 object, similar to srand() requires an argument to make a seed value.
  • It generates a random number (like rand()) using operator().

Program:

Let us take an example to illustrate the std::mt19937 class in C++.

Output:

Std::mt19937 class in C++

Why not use rand() instead of mt19937?

The rand() function doesn't work well for similar random numbers in the real world despite being completely inefficient. The random repetition of random numbers generated by rand() can be targeted by anyone, which is quite dangerous. On the other hand, std::mt19937 offers the following advantages.

  1. Unlike rand(), std::mt19937 also takes lot of time. The random sequence would take approximately 3684 × 105,985 years for a system generating the pseudo-random numbers to be iterative, assuming it can generate 1,000,000,000 (one billion) pseudo-random numbers per second using a Mersenne diverter. It is reasonable to assume that no applicant will ever receive a number.
  2. Multiple random number generators can be started simultaneously with different seed values.

Program:

Let us take another example to illustrate the std::mt19937 class in C++.

Output:

Std::mt19937 class in C++

Explanation:

Its member function is identical to mersenne_twister_engine in that it is a copy of the std::mersenne_twister_engine class. The following is a list of some of the most important membership functions.

1. (Constructor): Creator mt19937. Retrieves a seed sequence object (similar to the srand() function) or a seed value of the seed type.

Program:

Output:

Std::mt19937 class in C++

2. min(): Returns zero, which is the minimum value that operator() can return.

Program:

Let us take another example to illustrate the std::mt19937 class using the min() function in C++.

Output:

Std::mt19937 class in C++

3. max(): It returns the maximum value that operator() can return (232 - 1 = 4294967295).

Program:

Let us take another example to illustrate the std::mt19937 class using the max() function in C++.

Output:

Std::mt19937 class in C++

4. seed(): This function further modifies the seed value of the object by the seed value of the seed sequence object or result type.

Program:

Let us take another example to illustrate the std::mt19937 class using the seed() function in C++.

Output:

Std::mt19937 class in C++

5. operator(): It generates pseudo-random integers.(similar to function rand()).

Program:

Let us take another example to illustrate the std::mt19937 class using the operator() in C++.

Output:

Std::mt19937 class in C++




Latest Courses