Saturday 18 November 2017

C++ Program to Generate Random Numbers Using Middle Square Method


Code:

#include   iostream
#include   math.h
#include   stdlib.h

using namespace std;

int a[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
int middleSquareNumber(int numb, int dig)
{
    int sqn = numb * numb, next_num = 0;
    int trim = (dig / 2);
    sqn = sqn / a[trim];
    for (int i = 0; i < dig; i++)
    {
        next_num += (sqn % (a[trim])) * (a[i]);
        sqn = sqn / 10;
    }
    return next_num;
}

int main(int argc, char **argv)
{
    cout << "Enter the #-digit random numbers you want: ";
    int n;
    cin >> n;
    int start = 1, end = 1;

    start = a[n - 1];
    end = a[n];

    int number = ((rand()) % (end - start)) + start;
    cout << "The random numbers are:\n" << number << ", ";
    for (int i = 1; i < n; i++)
    {
        number = middleSquareNumber(number, n);
        cout << number << ", ";
    }
    cout << "...";
}


Output:

Enter the #-digit random numbers you want: 5
The random numbers are:
10041, 16426, 796264, -276041, -115546, ...


More C++ Programs:













100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...