Stdmt19937 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::mersennetwisterengine 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 mt1993764 64-bit version. Both are shown as mersennetwister_engine examples.

Syntax:

Example

mt19937 mt1 (seed_value);

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++.

Example

#include <iostream>
#include <random>
#include <ctime>
int main() {
    std::mt19937 mt(std::time(nullptr)); // Initializing with seed value
    std::cout << mt() << '\n'; // Generating and printing a random number
    return 0;
}

Output:

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.

  • 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.
  • 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++.

Example

#include <ctime>  
#include <iostream> 
#include <random> 
using namespace std;

// Driver code 
int main() 
{ 
mt19937 mt1(10000); 
mt19937 mt2(100000); 

cout << mt1() << endl; 
cout << mt2() << endl; 
return 0; 
}

Output:

Explanation:

Its member function is identical to mersennetwisterengine in that it is a copy of the std::mersennetwisterengine 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:

Example

// C++ program to demonstrate the std::mt19937 using the constructor
#include <ctime>  
#include <iostream> 
#include <random> 
using namespace std;
int main() {
    // Using the current time as seed
    std::mt19937 mt(std::time(nullptr));
    // Generating a random number
    std::cout << mt() << '\n';
    return 0;
}

Output:

  1. 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++.

Example

#include <iostream>
#include <random>
#include <limits>
int main() {
    std::cout << "the minimum integer it can generate is " <<
                 std::numeric_limits<std::mt19937::result_type>::min() << std::endl;
    return 0;
}

Output:

  1. 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++.

Example

#include <iostream>
#include <random>
#include <limits>
int main() {
    std::cout << "mt19937 can generate random numbers up to " <<
                 std::numeric_limits<std::mt19937::result_type>::max() << std::endl;
    return 0;
}

Output:

  1. 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++.

Example

#include <iostream>
#include <random>
int main() {
    // Defining the mt19937 object
    std::mt19937 mt;
    // Initializing a random sequence with a seed value
    mt.seed(45218965);
    std::cout << "Some random numbers generated by mt19937 are:\n";
    // Generating and printing random numbers
    for (int i = 5; i > 0; i--) {
        std::cout << mt() << ' ';
    }
    std::cout << std::endl;
    return 0;
}

Output:

  1. 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++.

Example

#include <iostream>
#include <random>
#include <ctime>
int main() {
    // Initializing mt19937 object with the current time as seed
    std::mt19937 mt(std::time(nullptr));
    // Generating and printing 5 random numbers
    for (int i = 0; i < 5; i++) {
        // operator() is used to generate random numbers
        std::cout << mt() << ' ';
    } 
    std::cout << std::endl;
    return 0;
}

Output:

Input Required

This code uses input(). Please provide values below: