Generate Random Numbers Using Middle Square Method in Java Last Updated : 20 Nov, 2020 Comments Improve Suggest changes Like Article Like Report This method was proposed by Van Neumann. In this method, we have a seed and then the seed is squared and its midterm is fetched as the random number. Consider we have a seed having N digits we square that number to get a 2N digits number if it doesn't become 2N digits we add zeros before the number to make it 2N digits. A good algorithm is basically the one which does not depend on the seed and the period should also be maximally long that it should almost touch every number in its range before it starts repeating itself as a rule of thumb remember that longer the period more random is the 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 In the above example, we can notice that we get some random numbers 19,36,29,84,05,02,00 which seem to be random picks, in this way we get multiple random numbers until we encounter a self-repeating chain. We also get to know a disadvantage of this method that is if we encounter a 0 then we get a chain of 0s from that point. Also, consider that we get a random number 50 the square will be 2500 and the midterms are 50 again, and we get into this chain of 50, and sometimes we may encounter such chains more often which acts as a disadvantage and because of these disadvantages this method is not practically used for generating random numbers. Implementation: Java // 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 + ", "); } } } OutputThe random numbers for the Geeks are: 325, 562, 584, 105, 102, 40, 160, 560, 360, 960, Note: The above program shows how the Middle Square Number method works you can run the program multiple times to see different random numbers generated every time. And this method is not recommended as an ideal way of generating random numbers due to its disadvantages but this method can be used as a hashing algorithm and some other applications too. Comment More infoAdvertise with us Next Article Generate Random Numbers Using Middle Square Method in Java H haridarshanc Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Program to Find Area of Square Using Method Overloading A square is a simple flat shape in a plane, defined by four points at the four corners. It has four sides with equal length and four corners with right angles. Method Overloading: Method overloading allows different methods to have the same name, but different signatures where the signature can diff 3 min read How to Generate Unique Positive Long Number in Java? Java provides different ways to generate random numbers, using some built-in methods and classes, but most of them do generate unique positive long numbers like java.util.Random class, Math.random method and ThreadLocalRandom class, and many others. But in most of these either with randomness unique 3 min read Different Methods to Check whether is the number is Square root or not in Java In this article, we will learn different methods to check whether the number is a square root or not in Java. Examples: Input: x = 4Output: 4 is a perfect square: True Input: x = 11Output: 3Explanation: 11 is a perfect square: False Methods to Check Square Root in JavaBelow are the methods by which 5 min read Java Program to Implement Park-Miller Random Number Generation Algorithm ParkâMiller random number generator is also known as Lehmer random number generator. A general formula of a random number generator (RNG) of this type is, Xk+1 = a * xk mod m Where the modulus m is a prime number or a power of a prime number, the multiplier a is an element of high multiplicative ord 3 min read Java Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31 2 min read BigDecimal sqrt() Method in Java with Examples The java.math.BigDecimal.sqrt(MathContext mc) is an inbuilt function added in Java SE 9 & JDK 9 which returns BigDecimal value of square root of a BigDecimal on which sqrt() method is applied with rounding according to the context settings. Syntax: public BigDecimal sqrt(MathContext mc) Paramete 2 min read Java Program to Generate a matrix having sum of secondary diagonal equal to a perfect square Given an integer N, the task is to generate a matrix of dimensions N x N using positive integers from the range [1, N] such that the sum of the secondary diagonal is a perfect square. Examples: Input: N = 3Output:1 2 32 3 13 2 1Explanation:The sum of secondary diagonal = 3 + 3 + 3 = 9(= 32). Input: 3 min read Java Program to Print Multiplication Table for Any Number Given a number n as input, we need to print its table, where N>0.Example:Input 1: N = 7Output: 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 7 * 10 = 70Ways to Print Multiplication Table for Any Number in JavaFour ways are shown to Print Multipl 4 min read Java Program To Multiply Two Numbers Represented By Linked Lists Given two numbers represented by linked lists, write a function that returns the multiplication of these two linked lists. Examples: Input: 9->4->6 8->4 Output: 79464 Input: 3->2->1 1->2 Output: 3852Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. 3 min read Java Program to Implement the Linear Congruential Generator for Pseudo Random Number Generation Linear Congruential Method is a class of Pseudo-Random Number Generator (PRNG) algorithms used for generating sequences of random-like numbers in a specific range. This method can be defined as: Xi+1 = aXi + c mod m where, X, is the sequence of pseudo-random numbers m, ( > 0) the modulus a, (0, m 3 min read Like