Java Program to Generate Random Numbers using Middle Square Method

«
»
This is java program to generate the random numbers. 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 Java Program to Generate Random Numbers Using Middle Square Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a sample program to generate random numbers using the middle square method
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Middle_Suqare_Method 
  7. {
  8.     static int a[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
  9.     static int middleSquareNumber(int numb, int dig)
  10.     {
  11.         int sqn = numb*numb, next_num=0;
  12.         int trim = (dig/2);
  13.         sqn = sqn / a[trim];
  14.         for(int i=0; i<dig; i++)
  15.         {
  16.             next_num += (sqn%(a[trim]))*(a[i]);
  17.             sqn = sqn/10;
  18.         }
  19.         return next_num;
  20.     }
  21.     public static void main(String args[])
  22.     {
  23.         System.out.println("Enter the #-digit random numbers you want: ");
  24.         Scanner sc = new Scanner(System.in);
  25.         int n = sc.nextInt();
  26.  
  27.         int start=1, end=1;
  28.  
  29.         start = a[n-1];
  30.         end = a[n]; 
  31.  
  32.         Random rand = new Random();
  33.         int number = rand.nextInt(end-start)+start;
  34.         System.out.print("The random numbers are:\n" +number+", ");
  35.         int new_number=0;
  36.         for(int i=0; i<9; i++)
  37.         {
  38.             number = Middle_Suqare_Method.middleSquareNumber(number, n);
  39.             System.out.print(number+", ");
  40.         }
  41.         System.out.print("...");
  42.  
  43.         sc.close();
  44.     }
  45. }

Output:

advertisement
$ javac Middle_Square_Method.java
$ java Middle_Square_Method
Enter the #-digit random numbers you want: 
2
The random numbers are:
89, 92, 46, 11, 12, 14, 19, 36, 29, 84, ...

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in Java 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 & technical discussions at Telegram SanfoundryClasses.