In mathematics, the middlesquare method is a method of generating pseudorandom numbers. In practice it is not a good method, since its period is usually very short and it has some severe weaknesses; repeated enough times, the middlesquare method will either begin repeatedly generating the same number or cycle to a previous number in the sequence and loop indefinitely.
YouTube Encyclopedic

1/3Views:2 880191 239565 969

✪ hashing methods  digit extraction & mid square part 3  by bhanu priya

✪ Completing the Square Method in Quadratic Equation (Hindi)  NCERT 10th Class Maths

✪ Factoring Trinomials The Easy Fast Way
Transcription
Contents
History
In Mathematics
The method was invented by John von Neumann, and was described at a conference in 1949.^{[1]}
In the 1949 talk, Von Neumann quipped that, "Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin." What he meant, he elaborated, was that there were no true "random numbers", just means to produce them, and "a strict arithmetic procedure", like the middlesquare method, "is not such a method." Nevertheless he found these methods hundreds of times faster than reading "truly" random numbers off punch cards, which had practical importance for his ENIAC work. He found the "destruction" of middlesquare sequences to be a factor in their favor, because it could be easily detected: "one always fears the appearance of undetected short cycles."^{[1]} Nicholas Metropolis reported sequences of 750,000 digits before "destruction" by means of using 38bit numbers with the "middlesquare" method.^{[2]}
First Invention Theory
The book The Broken Dice by Ivar Ekeland gives an extended account of how the method was invented by a Franciscan friar known only as Brother Edvin sometime between 1240 and 1250.^{[3]} Supposedly, the manuscript is now lost, but Jorge Luis Borges sent Ekeland a copy that he made at the Vatican Library.
The Method
To generate a sequence of ndigit pseudorandom numbers, an ndigit starting value is created and squared, producing a 2ndigit number. If the result has fewer than 2n digits, leading zeroes are added to compensate. The middle n 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.
The value of n must be even in order for the method to work if the value of n is odd then there will not necessarily be a uniquely defined 'middle ndigits' to select from. Consider the following: If a 3digit number is squared it can yield a 6 digit number (eg: 540^{2} = 291600). If there were to be a middle three digit that would leave 6  3 = 3 digits to be distributed to the left and right of the middle. It is impossible to evenly distribute these digits equally on both sides of the middle number and therefore there are no 'middle digits.' It is acceptable to pad the seeds with zeros to the left in order to create an even valued ndigit (eg: 540 → 0540).
For a generator of ndigit numbers, the period can be no longer than 8^{n}. If the middle n digits are all zeroes, the generator then outputs zeroes forever. If the first half of a number in the sequence is zeroes, the subsequent numbers will be decreasing to zero. While these runs of zero are easy to detect, they occur too frequently for this method to be of practical use. The middlesquared method can also get stuck on a number other than zero. For n = 4, this occurs with the values 0100, 2500, 3792, and 7600. Other seed values form very short repeating cycles, e.g., 0540 → 2916 → 5030 → 3009. These phenomena are even more obvious when n = 2, as none of the 100 possible seeds generates more than 14 iterations without reverting to 0, 10, 50, 60, or a 24 ↔ 57 loop.
Example Implementation
Here, the algorithm is rendered in Python 3.
seed_number = int(input("Please enter a four digit number:\n[####] "))
number = seed_number
already_seen = set()
counter = 0
while number not in already_seen:
counter += 1
already_seen.add(number)
number = int(str(number * number).zfill(8)[2:6]) # zfill adds padding of zeroes
print(f"#{counter}: {number}")
print(f"We began with {seed_number}, and"
f" have repeated ourselves after {counter} steps"
f" with {number}.")
Middle Square Weyl Sequence PRNG
The defects associated with the original middlesquare generator can be rectified by running the middle square with a Weyl sequence^{[4]}^{[5]}. The Weyl sequence prevents convergence to zero. The Weyl sequence also prevents the repeating cycle problem described above. A C implementation is shown below.
#include <stdint.h>
uint64_t x = 0, w = 0, s = 0xb5ad4eceda1ce2a9;
inline static uint32_t msws() {
x *= x;
x += (w += s);
return x = (x>>32)  (x<<32);
}
The Weyl sequence (w += s) is added to the square of x. The middle is extracted by shifting right 32 bits. This generator passes BigCrush^{[6]}^{[7]}. and PractRand^{[8]}. This may be the world's fastest PRNG for producing 32bit floatingpoint numbers^{[4]}. A free software package is available here^{[5]}.
References
 ^ ^{a} ^{b} The 1949 papers were not reprinted until 1951. John von Neumann, “Various techniques used in connection with random digits,” in A.S. Householder, G.E. Forsythe, and H.H. Germond, eds., Monte Carlo Method, National Bureau of Standards Applied Mathematics Series, vol. 12 (Washington, D.C.: U.S. Government Printing Office, 1951): pp. 3638.
 ^ Donald E. Knuth, The art of computer programming, Vol. 2, Seminumerical algorithms, 2nd edn. (Reading, Mass.: AddisonWesley, 1981), ch. 3, section 3.1.
 ^ Ivar Ekeland (15 June 1996). The Broken Dice, and Other Mathematical Tales of Chance. University of Chicago Press. ISBN 9780226199924.
 ^ ^{a} ^{b} Widynski, Bernard (April 2017). "Middle Square Weyl Sequence RNG". arXiv:1704.00358v3.
 ^ ^{a} ^{b} Middle Square Weyl Sequence RNG website.
 ^ Pierre L’Ecuyer & Richard Simard (2007), "TestU01: A Software Library in ANSI C for Empirical Testing of Random Number Generators", ACM Transactions on Mathematical Software, 33: 22.
 ^ The TestU01 web site.
 ^ Chris DotyHumphrey (2018), "Practically Random: C++ library of statistical tests for RNGs." version 0.94.