Generate Random Numbers Using Middle-Square Method in C++



The middle-square method is one of the simplest methods of generating random numbers. This method will either begin repeatedly generating the same number or cycle to a previous number in the sequence and loop indefinitely. For a generator of n-digit random numbers, the period can be no longer than the specified n(number of digits). If the middle n digits are all zeroes, the generator then generates zeroes forever.

In this article, we will implement a C++ program to generate ten 4-digits random number using the middle-square method.

Steps for Middle-Square Random Number Generation

The steps for generating random numbers using the middle-square method are as follows:

  • We have defined an array a[] that is later used to remove and extract the specified number of digits from the generated number.
  • The middleSquareNumber() function is used to generate a random number of specified digits based on the previous number. It takes two arguments: the number and the number of digits.
  • Inside the function, using sqn = sqn/a[t], we remove the extra digits and bring the middle digits to the front.
  • The for loop then extracts n number of middle digits and reconstructs the next number from them and the next_number is returned.
  • In the main() function, we have used the srand(time(0)) function to ensure different results is generated on each run.
  • The if/else statement checks that n(digit) is within the range of 1 to 8. A random number is generated using the rand() function in this defined range.
  • The for loop calls the middleSquareNumber() function to generate 10 random numbers.

C++ Program for Middle-Square Random Number Generation

The following code implements above steps using the middle-square method to generate 4-digits random numbers.

Open Compiler
#include <iostream> #include <cstdlib> // For rand() and srand() #include <ctime> // For time() to seed rand() using namespace std; int a[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; int middleSquareNumber(int number, int digit) { int sqn = number * number; int next_number = 0; int t = digit / 2; sqn = sqn / a[t]; // Shift the squared number to get middle digits for (int i = 0; i < digit; i++) { next_number += (sqn % 10) * a[digit - i - 1]; // Get the middle digits and rebuild the next number sqn = sqn / 10; } return next_number; } int main() { // Seed the random number generator with current time srand(time(0)); // Set the number of digits int n = 4; // Ensure 'n' is within bounds of the array a[] (1 to 8 digits) if (n < 1 || n > 8) { cout << "Please set 'n' between 1 and 8." << endl; return 1; } // Calculate the range for the random number based on 'n' int start = a[n - 1]; int end = a[n]; // Generate the first random number within the desired range int number = (rand() % (end - start)) + start; cout << "The " << n << " digit random numbers are:\n" << number << ", "; for (int i = 1; i < 10; i++) { // Generate and print the next 9 random numbers number = middleSquareNumber(number, n); cout << number << ", "; } cout << "..." << endl; return 0; }

The output of the above code is:

The 4 digit random numbers are:
5629, 6586, 3573, 3667, 8644, 7817, 4501, 952, 3609, 8420, ...
Updated on: 2025-04-22T11:38:27+05:30

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements