How to generate random numbers using middle square method in Java

This method was proposed by Van Neumann. In this method, we have a seed and then the seed is squared and the center is extracted as an arbitrary number. Think we have a seed with N digits, we square that number to get 2N digits, if they don't become 2N digits, we add zeros in front of the number to make it 2N digits. A good algorithm is basically one that doesn't depend on seeds, and the time period should also be as long as possible, so it usually has to hit nearly every number in its range before it starts looping. Keep in mind that longer time periods are more of a random number.

Example: 

Consider the seed to be 14 and we want a two digit random number.
Number --> Square --> Mid-term
14     --> 0196   --> 19 
19     --> 0361   --> 36
36     --> 1296   --> 29
29     --> 0841   --> 84
84     --> 7056   --> 05
05     --> 0025   --> 02
02     --> 0004   --> 00
00     --> 0000   --> 00 
</pre?

In the above example, we can notice that we got some random numbers 19,36,29,84,05,02,00 seems to be a random selection, so we get multiple random numbers, until we encounter a self-repeat Chain. We also understand a disadvantage of this method, that is, if we encounter 0, then we will get a string of 0 from that point. In addition, considering that we get a random number 50, the square will be 2500, and the midterm exam will be 50 again. We have entered the chain of 50. Sometimes we may encounter such chains more frequently. This is a disadvantage because of these Disadvantages This method is not actually used to generate random numbers. implement:

 

// Generate Random Numbers Using Middle
// Square Method in Java
import java.util.Random;

public class Main {
	static int rangeArray[]
		= { 1,	 10,	 100,	 1000,	 10000,
			100000, 1000000, 10000000, 100000000 };
	// function for generating a random number
	static long middleSquareNumber(long num, int digit)
	{
		long sqn = num * num, nextNum = 0;
		int trim = (digit / 2);
		sqn = sqn / rangeArray[trim];
		for (int i = 0; i < digit; i++) {
			nextNum += (sqn % (rangeArray[trim]))
					* (rangeArray[i]);
			sqn = sqn / 10;
		}
		return nextNum;
	}
	public static void main(String args[])
	{
		int numberOfDigit = 3;
		int start = rangeArray[numberOfDigit - 1],
			end = rangeArray[numberOfDigit];
		// create rand object
		Random rand = new Random();
		long nextNumber = rand.nextInt(end - start) + start;
		System.out.print(
			"The random numbers for the Geeks are:\n"
			+ nextNumber + ", ");
		// Generating 10 random numbers
		for (int i = 0; i < 9; i++) {
			nextNumber = middleSquareNumber(nextNumber,
											numberOfDigit);
			System.out.print(nextNumber + ", ");
		}
	}
}

Output

The random numbers for the Geeks are:
325, 562, 584, 105, 102, 40, 160, 560, 360, 960, 

Note: The above program shows how the intermediate square number method works, and you can run the program multiple times to see the different random numbers generated each time. Due to its shortcomings, this method is not recommended as an ideal method for generating random numbers, but it can also be used as a hash algorithm and some other applications.

Submit Your Programming Assignment Details