The pseudo-random numbers can be generated from Middle-Square Method. In this post, we will look at the way the middle square method works and will also look at its Code in C.
Contents
Implementation of Mid-Square Method:
The mid-square method works by taking the middle term of the squared number of the seed. Leading 0 can be added if necessary.
Example of the random number generation using Mid-Square Method:
Let’s take a seed n=62 Then, n2=3844
Now, taking the middle two-term the new seed is n=84. (i.e. We removed the first and last digit to get the middle term.
Now, new seed n=84, n2=7056,
so the new seed is n=5
Let’s take another example where we have to add leading 0:
seed(n)=25,
n2=625
Then to get the 2-digit seed from the square, we can add leading 0 as below:
n2=0625,
So, seed(n) =62, n2=3844,
Then the new seed becomes 84 and so on.
Mid Square Method Doesn’t Work for Odd Length Seed.
The mid-square method for random number generation only works if the number of digit of the seed is even. It is because if the seed is of odd length, then from its square, not always, the exact middle term of the same length of seed cannot be extracted.
Also Read: C Program to Implement BubbleSort with Function
For example: when seed of odd length is taken
For example, let’s take a number of odd length n=674
It has digit length 3, now n2=454,276
As you can see the middle term having 3 digits cannot be extracted from here.
Explanation of Code:
Before I head into code, I just want to explain what I have done in the following Code.
1) A seed is taken and the number of digits in the seed is calculated as if it has odd length the mid-square method cannot be applied. The digit is copied to count as well. So, count has the original length of the entered seed.
2) If the seed has an odd length then a message “mid-square method cannot be applied in odd length is applied.” is shown.
However, if the seed has even length, then the square of the seed is calculated and again the digit present in the square is calculated.
3)Then it is divided by 10, ceil(digit/4.0) times to remove the least significant values from the seed.
4) Then, the seed after dividing(seed from step 3) mod (10 ^count) (count: original length of seed) is calculated to get the final seed.
Example:
1) let’s take a number seed=5865, here the number of digits in the seed=4, which is even. So digit=4, count=digit=4
2) So mid-square method can be applied here. So the square of seed is calculated. (5856)²=34398225, Here digit=8
3) Now, 34398225 is to be divided by 10, ceil(digit/4.0) times i.e. ceil(8/4.0)=2 times.
So, 34398225/10/10= 343982
4) Seed from step 3 mod (10^count) is to be done now where count is the original length of the entered seed.
So, seed from step 3=343982,
10^count=10^4=10000
Then, 343982 mod 10000=3982
Code in C:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void intermediate_odd(long long,int);
int count_digit(long long, int);
int main()
{
int a[20]; /*to store the random numbers */
long long seed; /*seed is the initaial number */
int digit; /*to know the number of digit in the square obtained */
int n; /*how many random numbers to generate */
int i,j; /*to control the loop*/
long long p;/* to copy something */
int count; /*to count the original length of the seed which will be used as remainder */
int divisor; /* use this to divide and find remainder*/
printf("Enter a number to generate the random number: ");
scanf("%lld",&seed);
p=seed;
digit=0;
digit=count_digit(p,digit);
count=digit; /*the same digit is to be used with 10 power to get reaminder */
if(digit%2!=0) /*if the number of digits is odd then the mid square multiplicate method cannot be applied */
{
printf("\nThe mid square multipicate method cannot be applied as the number of digit of seed is %d(odd)",digit);
}
else /*if even */
{
printf("\n How many random number you want to generate: ");
scanf("%d",&n);
printf("\nThe random numbers are:\n");
for(i=0;i<n;i++)
{
seed=seed*seed;
p=seed;
digit=0;
digit=count_digit(p,digit);
digit=ceil(digit/4.0);
for(j=0;j<digit;j++)
{
seed=seed/10;
}
divisor=pow(10,count);
seed=seed%divisor;
a[i]=seed;
printf("%lld\t",seed);
digit=0;/* checking if the intermediate seed has odd length */
p=seed;
digit=count_digit(p,digit);
if(digit%2!=0)
{
intermediate_odd(seed,digit);
exit(0);
}
}
}
getch();
return(0);
}
int count_digit(long long seed,int digit) //to count the number of digit
{
long long p;
p=seed;
while(p!=0) /*digit count*/
{
p=p/10;
digit++;
}
return digit;
}
void intermediate_odd(long long seed, int digit)
{
printf("\nThe intermediate seed %d has odd length %d and therefore mid square method cannot be applied from here",seed,digit);
}
Outputs:
Also Read: C Program to Implement Selection Sort
Do comment if you have any queries or if you are confused in any steps.