programming-examples/c/Numerical/C Program to Generate Random Numbers Using Middle Square Method.c
2019-11-15 12:59:38 +01:00

53 lines
1.3 KiB
C

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
unsigned long long int randm(int n);
unsigned long long int von(unsigned long long int x, int n);
int main(void)
{
unsigned long long int x, s;
int n, i, r;
printf("Enter the number of digits in the seed value ");
scanf("%d", &n);
printf("\nEnter the total number of random numbers to be generated ");
scanf("%d", &r);
if (n >= 12)
{
printf("TOO LARGE!!");
exit(0);
}
x = randm(n);
for(i = 0; i < r; i++)
{
s = von(x, n);
x = s;
printf("\nRandom Number generated: %lld\n", s);
}
return 0;
}
/*Generating Random Number of desired digit*/
unsigned long long int randm(int n)
{
double x;
unsigned long long int y;
srand(getpid());
x = rand() / (double)RAND_MAX;
y = (unsigned long long int) (x * pow(10.0, n*1.0));
return y;
}
/*Calculating Random Number By Von Neumann Middle Square method*/
unsigned long long int von(unsigned long long int x, int n)
{
unsigned long long int y;
int k;
k = n / 2;
y =(unsigned long long int)((x / pow(10.0, k * 1.0)) * x) % (unsigned long long int) (pow(10.0, n * 1.0));
return y;
}