How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the C++ programming language

Table of Contents

Problem Statement

numbers as shown above. are as shown above
repetitions of Is as follows:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the C++ programming language

The provided code is a C++ implementation of a random number generator (RNG) using the Mersenne Twister algorithm. The Mersenne Twister is a pseudo-random number generator that is known for its long period and good randomness.

The code defines two classes: mod and RNG. The mod class is a simple function that performs modular arithmetic on two integers. The RNG class is the main random number generator class.

The RNG class has three member variables: a1, m1, and x1. The a1 array is used to store the coefficients of the first generator polynomial. The m1 variable is used to store the modulus of the first generator. The x1 array is used to store the state of the first generator.

The RNG class also has three member variables: a2, m2, and x2. These variables are used for the second generator.

The seed method of the RNG class is used to initialize the state of the generator. The next_int method of the RNG class is used to generate a random integer. The next_float method of the RNG class is used to generate a random floating-point number.

The main function of the code demonstrates how to use the RNG class. First, the main function creates an instance of the RNG class. Then, the main function calls the seed method of the RNG class to initialize the state of the generator.

After initializing the state of the generator, the main function calls the next_int method of the RNG class to generate a random integer. The main function then prints the random integer to the console.

The main function then calls the next_int method of the RNG class to generate four more random integers. The main function then prints the four random integers to the console.

Next, the main function creates an array of five integers to store the counts of each random number generated. The main function then calls the seed method of the RNG class to re-initialize the state of the generator.

After re-initializing the state of the generator, the main function calls the next_float method of the RNG class to generate 100,000 random floating-point numbers. The main function then increments the count of the corresponding random number in the array of counts.

Finally, the main function prints the counts of each random number to the console.

Source code in the cpp programming language

#include <array>
#include <iostream>

int64_t mod(int64_t x, int64_t y) {
    int64_t m = x % y;
    if (m < 0) {
        if (y < 0) {
            return m - y;
        } else {
            return m + y;
        }
    }
    return m;
}

class RNG {
private:
    // First generator
    const std::array<int64_t, 3> a1{ 0, 1403580, -810728 };
    const int64_t m1 = (1LL << 32) - 209;
    std::array<int64_t, 3> x1;
    // Second generator
    const std::array<int64_t, 3> a2{ 527612, 0, -1370589 };
    const int64_t m2 = (1LL << 32) - 22853;
    std::array<int64_t, 3> x2;
    // other
    const int64_t d = (1LL << 32) - 209 + 1; // m1 + 1

public:
    void seed(int64_t state) {
        x1 = { state, 0, 0 };
        x2 = { state, 0, 0 };
    }

    int64_t next_int() {
        int64_t x1i = mod((a1[0] * x1[0] + a1[1] * x1[1] + a1[2] * x1[2]), m1);
        int64_t x2i = mod((a2[0] * x2[0] + a2[1] * x2[1] + a2[2] * x2[2]), m2);
        int64_t z = mod(x1i - x2i, m1);

        // keep last three values of the first generator
        x1 = { x1i, x1[0], x1[1] };
        // keep last three values of the second generator
        x2 = { x2i, x2[0], x2[1] };

        return z + 1;
    }

    double next_float() {
        return static_cast<double>(next_int()) / d;
    }
};

int main() {
    RNG rng;

    rng.seed(1234567);
    std::cout << rng.next_int() << '\n';
    std::cout << rng.next_int() << '\n';
    std::cout << rng.next_int() << '\n';
    std::cout << rng.next_int() << '\n';
    std::cout << rng.next_int() << '\n';
    std::cout << '\n';

    std::array<int, 5> counts{ 0, 0, 0, 0, 0 };
    rng.seed(987654321);
    for (size_t i = 0; i < 100000; i++) 		{
        auto value = floor(rng.next_float() * 5.0);
        counts[value]++;
    }
    for (size_t i = 0; i < counts.size(); i++) 		{
        std::cout << i << ": " << counts[i] << '\n';
    }

    return 0;
}


  

You may also check:How to resolve the algorithm Collections step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the Racket programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the Delphi programming language
You may also check:How to resolve the algorithm String length step by step in the Liberty BASIC programming language