### Problem:

Mid square method, mid square random number generator Code in MatLab and C or C++.

### Solution:

#### Mid Square Random Number Generator Code in C:

#include<stdio.h>
int main()
{
long int seed = 6897; int i, n, random;
printf(“How many number you want to generate : “);
scanf(“%d”, &n);
printf(“Random Numbers are : “);
for(i = 0; i < n; i++)
{
seed = seed * seed;
seed = seed / 100; seed = seed % 10000; random = seed;
printf(“%d “, random);
}
printf(“\n”);
return 0;
}

Output of the Mid Square Random Number Generator code in C:

### Mid Square Random Number Code in Matlab:

%% Problem : Random Number Generator —> Mid Square Method %%
% Author : Maniruzzaman Akash
totalNumber = input(‘Number of Random Numbers want to generate : ‘);
choose = input(‘Enter the seed : ‘);
disp(‘Total Random Numbers are : ‘);
for i = 1:totalNumber
random = choose ^ 2;
random = random / 100; % Find the dividend
random = rem(random, 10000); % Get the reminder;
choose = random;
fprintf(‘%.2f ‘, random);
end
fprintf(‘\n’);

### Procedure of Mid Square Method Random Number Generator:

1. Take the first seed or first random number.
2. Square it.
3. Take the middle from the 8 digits square value
4. divide it by 100 first then
5. take the reminder of 10000 of that value again
6. Finally result will present in the seed or in random.

### Tags:

Numerical Methods, Simulation and Modeling, C Programming, MatLab Code, Mid Square Method Code, Midsquare code implementation,

You are watching: Mid Square Method Code implementation in C and MatLab. Info created by GBee English Center selection and synthesis along with other related topics.