Middle Square Method
Middle Square Method
Random numbers can be generated from a calculation of mathematical formulas. Such random numbers
are often referred to as pseudo random numbers. The numbers produced appear to be statistically
random, despite having been produced by a completely deterministic and repeatable process.
We use methods like Middle-square method because many sources of randomness available to humans,
such as rolling dice, rely on physical processes not readily available to computer programs.
Before modern computing, researchers requiring random numbers would either generate them through
various means (dice, cards, roulette wheels, etc.) or use existing random number tables.
In the late 1940s, many physicists, such as Von Neumann and Stanislaw Ulam, the primary developer of
the Monte Carlo method, were planning the first computer simulations of nuclear fission.
This work was part of a postwar effort to build ever-more-ghastly nuclear weapons.
When a neutron strikes an atomic nucleus, it can either bounce off or be absorbed; in some
cases, absorption triggers a fission event, splitting the nucleus and releasing more neutrons. The
bomb explodes only if the population of neutrons grows rapidly enough.
Random numbers were needed for a simulation of neutrons diffusing through various
components of a bomb assembly.
The simulation technique, in which random numbers determined the fates of thousands of individual
neutrons, came to be known as the Monte Carlo method. The name comes from the Monte Carlo
Casino in Monaco, where the primary developer of the method, physicist Stanislaw Ulam, was inspired
by his uncle's gambling habits.
Computers of that time used punch cards for storage, which made the usage of precomputed
tables of random numbers really inefficient, since such tables would require huge amount of
storage and that storage was slow.
In order to overcome this hurdle, the middle-square method was invented by John von
Neumann in 1947, and was described by him at a conference in 1949.
Von Neumann elaborated that there were no true "random numbers", just means to produce
them, and "a strict arithmetic procedure", like the middle-square method, "is not such a
method".
Nevertheless, he found these methods hundreds of times faster than reading "truly" random
numbers off punch cards.
The first version of the Monte Carlo program, run in the spring of 1948, used middle-square
random numbers.
Method
To generate a sequence of n-digit pseudorandom number:
1. n-digit starting value is taken. This number is called the seed, and is taken as the initial number
2. This number is then squared, producing a 2n-digit number.
o If the result has fewer than 2n-digits, leading zeroes are added to compensate.
3. The middle n-digits of the result would be next number in the sequence and is returned as a
result.
4. This process (step 2 and 3) is repeated to generate more numbers. The output of step 3
becomes the next seed.
for i = 1:n
square = seed * seed;
% Get rid of the last two digits
% For example, 1490841 will become 14908
square = floor(square / 100);
% Get rid of the digits other than the least significant four digits
% For example, 14908 will become 4908
square = mod(square, 10000);
% For the next iteration, the seed will be the new number
seed = square;
disp(seed);
end
Output:
Limitations:
Although the Middle-Square method is very fast and simple, it has one major disadvantage.
The sequence of numbers generated using this method is limited. Most sequences either degenerate to
zero, converge on a constant, or cycle forever through a short loop.
For example, in the experiment of estimating the value of π using Monte Carlo Method, this method can
only provide a limited number of random values.
Suppose you are using the random numbers to estimate the value of π by choosing random
points in a square and determining whether they fall inside an inscribed circle.
Let each random number specify the two coordinates of a single point.
Let’s assume it provides 2000 random points before repeating those points again. The first
Monte Carlo implementation used 2000 random values using the Middle-Square method.
After running through the 2,000 middle-square values you will have achieved a certain accuracy
in estimating the value of π.
But repeating the same values is pointless: It will never improve the accuracy because you will
simply be revisiting all the same points.
As a result, this method is not used in favour of other methods like Linear Congruence method for
random number generation, which are capable of generating even millions of random numbers before
having to repeat.