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:
-
Class Definition:
- There is a class named
MiddleSquare
that encapsulates the necessary operations to generate pseudorandom numbers using the Middle Square Method.
- There is a class named
-
Private Members:
state
: An unsigned long variable that represents the current state of the PRNG. It holds the previously generated random number.div
andmod
: These are also unsigned long variables used for mathematical operations within the Middle Square Method.
-
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
andmod
based on the givenlength
and initializesstate
with thestart
value.
- The default constructor is deleted using
-
next()
Method:- This method generates the next pseudorandom number using the Middle Square Method.
- It squares
state
, divides the result bydiv
, and then takes the remainder bymod
. 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.
-
main()
Function:- In the
main
function:- An instance of
MiddleSquare
is created with an initial random number675248
and a length of 6. - It iterates 5 times, generating and printing the next 5 pseudorandom numbers.
- An instance of
- In the
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