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:

Hướng dẫn Java Design Pattern – Object Pool
Java Program to Find the GCD and LCM of two Numbers
Spring Boot - Securing Web Applications
Java Program to Solve any Linear Equations
Java Program to Implement Self organizing List
Java Program to Implement TreeSet API
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Spring Boot - Cloud Configuration Server
Spring – Injecting Collections
Introduction to Spring Method Security
Spring Cloud – Tracing Services with Zipkin
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Class Loaders in Java
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Performance Difference Between save() and saveAll() in Spring Data
Java Program to Implement RenderingHints API
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Implement Fenwick Tree
Quick Guide to the Java StringTokenizer
HashMap trong Java hoạt động như thế nào?
Spring Cloud – Securing Services
Handling Errors in Spring WebFlux
Java Program to Implement Variable length array
Tính kế thừa (Inheritance) trong java
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Auditing with JPA, Hibernate, and Spring Data JPA
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Program to Find Path Between Two Nodes in a Graph
Java 9 Stream API Improvements
Use Liquibase to Safely Evolve Your Database Schema
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Collect a Java Stream to an Immutable Collection