The limitations of the middle square method in generating pseudo-random numbers.

1.2k Views Asked by At

I have been trying to research the middle square method as a way to generate pseudo-random numbers for a school project. Everywhere I look claims that this method is limited by its short period and will either loop or repeat if used enough times. However, I don't really understand how this happens or even why it happens. I wrote a Python script that takes a seed of 6 digits and I have yet to witness any looping or repetition. Is this because my seed is too large? If so, what digit seed would be appropriate to show the limitations of this method? Lastly, why does this method apparently repeat or loop if done enough times? Thank you!

2

There are 2 best solutions below

0
On

Since your internal state has a fixed size (it's an $n$-digit number), there are only finitely many ($10^n$) possible states, so if you iterate enough ($10^n+1$ times) you'll inevitably repeat an internal state. Since the method is deterministic, the sequence will repeat from that point onward. This is true of all PRNG algorithms, but good ones have internal states much larger than the numbers being generated. (E.g. using a 10-digit state while only outputting the last 5 digits of each state in order to generate 5-digit numbers is better than using a 5-digit state.) You should be able to explore these ideas (with sufficiently small seeds) using your Python script.

0
On

Here's a simple example - let's say we are taking the middle two digits of a four digit number and start with a seed of 99.

99 x 99 = 9801

Next seed is 80

80 x 80 = 6400

Next seed is 40

40 x 40 = 1600

Next seed is 60

60 x 60 = 3600 - so the seed is now stuck at 60 for ever.