C Program to Generate Random Numbers using Middle Square Method

This C program generates random numbers using middle square method. In mathematics, the middle-square method is a method of generating ‘pseudorandom’ numbers.To generate a sequence of 4-digit pseudorandom numbers, a 4-digit starting value is created and squared, producing an 8-digit number (if the result is less than 8 digits, leading zeroes are added to compensate). The middle 4 digits of the result would be the next number in the sequence, and returned as the result. This process is then repeated to generate more numbers.

Here is the source code of the C program to generate random numbers of desired length using Von Neumann middle square method. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. unsigned long long int randm(int n);
  5. unsigned long long int von(unsigned long long int x, int n);
  6.  
  7. int main(void)
  8. {
  9.   unsigned long long int x, s;
  10.   int n, i, r;
  11.  
  12.   printf("Enter the number of digits in the seed value ");
  13.   scanf("%d", &n);
  14.  
  15.   printf("\nEnter the total number of random numbers to be generated "); 
  16.   scanf("%d", &r);
  17.  
  18.   if (n >= 12){
  19.     printf("TOO LARGE!!");
  20.     exit(0);
  21.   }
  22.  
  23.   x = randm(n);
  24.   for(i = 0; i < r; i++){    
  25.      s = von(x, n);
  26.      x = s;
  27.   printf("\nRandom Number generated: %lld\n", s);
  28.   }
  29.   return 0;
  30. }
  31.  
  32.  
  33. /*Generating Random Number of desired digit*/
  34.  
  35. unsigned long long int randm(int n)
  36. {
  37.   double x;
  38.   unsigned long long int y;
  39.   srand(getpid());
  40.   x = rand() / (double)RAND_MAX;
  41.   y = (unsigned long long int) (x * pow(10.0, n*1.0));
  42.   return y;
  43. }
  44.  
  45.  
  46. /*Calculating Random Number By Von Neumann Middle Square method*/
  47.  
  48. unsigned long long int von(unsigned long long int x, int n)
  49. {
  50.   unsigned long long int y;
  51.   int k;
  52.   k = n / 2;
  53.   y =(unsigned long long int)((x / pow(10.0, k * 1.0)) * x) % (unsigned long long int) (pow(10.0, n * 1.0));
  54.   return y;
  55. }

$ gcc middle_square_method.c -o middle_square_method
$ ./middle_square_method
 
Enter the number of digits in the seed value 11
Enter the total number of random numbers to be generated 4
Random Number generated: 89135450408
 
Random Number generated: 85194370272
 
Random Number generated: 7260426368
 
Random Number generated: 37910451496

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.