How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the C++ programming language

Table of Contents

Problem Statement

To generate a sequence of n-digit pseudorandom numbers, an n-digit starting value is created and squared, producing a 2n-digit number. If the result has fewer than 2n digits, leading zeroes are added to compensate. The middle n digits of the result would be the next number in the sequence and returned as the result. This process is then repeated to generate more numbers. numbers (6 digits) as shown above.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the C++ programming language

The provided code is a C++ implementation of the Middle Square Method, a pseudorandom number generator (PRNG). Let's break down the code step by step:

  1. Class Definition:

    • There is a class named MiddleSquare that encapsulates the necessary operations to generate pseudorandom numbers using the Middle Square Method.
  2. Private Members:

    • state: An unsigned long variable that represents the current state of the PRNG. It holds the previously generated random number.
    • div and mod: These are also unsigned long variables used for mathematical operations within the Middle Square Method.
  3. Constructors:

    • The default constructor is deleted using delete.
    • There is a parameterized constructor that takes two arguments:
      • start: The initial random number used to seed the PRNG.
      • length: The length of the squared number (in digits) used for the next random number generation. It must be an even number.
      • Inside the constructor, it calculates div and mod based on the given length and initializes state with the start value.
  4. next() Method:

    • This method generates the next pseudorandom number using the Middle Square Method.
    • It squares state, divides the result by div, and then takes the remainder by mod. This operation effectively produces a number within the range [0, mod-1].
    • The generated random number is then stored in state for the next iteration.
  5. main() Function:

    • In the main function:
      • An instance of MiddleSquare is created with an initial random number 675248 and a length of 6.
      • It iterates 5 times, generating and printing the next 5 pseudorandom numbers.

Source code in the cpp programming language

#include <exception>
#include <iostream>

using ulong = unsigned long;

class MiddleSquare {
private:
    ulong state;
    ulong div, mod;
public:
    MiddleSquare() = delete;
    MiddleSquare(ulong start, ulong length) {
        if (length % 2) throw std::invalid_argument("length must be even");
        div = mod = 1;
        for (ulong i=0; i<length/2; i++) div *= 10;
        for (ulong i=0; i<length; i++) mod *= 10;
        state = start % mod;
    }
    
    ulong next() { 
        return state = state * state / div % mod;
    }
};

int main() {
    MiddleSquare msq(675248, 6);
    for (int i=0; i<5; i++)
        std::cout << msq.next() << std::endl;
    return 0;
}


  

You may also check:How to resolve the algorithm Table creation/Postal addresses step by step in the Phix programming language
You may also check:How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the 11l programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Delphi programming language
You may also check:How to resolve the algorithm Sort stability step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the Objeck programming language