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.

//This is a sample program to generate random numbers using the middle square method
import java.util.Random;
import java.util.Scanner;
 
 
public class Middle_Suqare_Method 
{
    static int a[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
    static int middleSquareNumber(int numb, int dig)
    {
        int sqn = numb*numb, next_num=0;
        int trim = (dig/2);
        sqn = sqn / a[trim];
        for(int i=0; i<dig; i++)
        {
            next_num += (sqn%(a[trim]))*(a[i]);
            sqn = sqn/10;
        }
        return next_num;
    }
    public static void main(String args[])
    {
        System.out.println("Enter the #-digit random numbers you want: ");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
 
        int start=1, end=1;
 
        start = a[n-1];
        end = a[n]; 
 
        Random rand = new Random();
        int number = rand.nextInt(end-start)+start;
        System.out.print("The random numbers are:\n" +number+", ");
        int new_number=0;
        for(int i=0; i<9; i++)
        {
            number = Middle_Suqare_Method.middleSquareNumber(number, n);
            System.out.print(number+", ");
        }
        System.out.print("...");
 
        sc.close();
    }
}

Output:

$ 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, ...

Related posts:

Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Implement Dijkstra’s Algorithm using Queue
Hướng dẫn Java Design Pattern – Bridge
“Stream has already been operated upon or closed” Exception in Java
Introduction to Apache Commons Text
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
An Example of Load Balancing with Zuul and Eureka
Java Program to Perform the Sorting Using Counting Sort
XML-Based Injection in Spring
Giới thiệu Google Guice – Binding
Immutable ArrayList in Java
Spring Boot Actuator
Java Program to Find Transitive Closure of a Graph
Java Program to Implement Kosaraju Algorithm
Handle EML file with JavaMail
Spring 5 Testing with @EnabledIf Annotation
Java Program to Implement Heap Sort Using Library Functions
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
A Quick Guide to Spring Cloud Consul
Spring 5 WebClient
How to Delay Code Execution in Java
Java Program to Check whether Undirected Graph is Connected using DFS
Introduction to Java Serialization
Spring Data JPA @Query
ETL with Spring Cloud Data Flow
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Changing Annotation Parameters At Runtime
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Enum trong java
Java Program to Implement Sorted Array
Hướng dẫn Java Design Pattern – Observer
Spring Security Registration – Resend Verification Email