Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Middle-square method
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== The method == To generate a sequence of ''n''-digit pseudorandom numbers, an ''n''-digit starting value is created and squared, producing a 2''n''-digit number. If the result has fewer than 2''n'' digits, [[leading zero]]es 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{{snd}} if the value of ''n'' is odd, then there will not necessarily be a uniquely defined "middle ''n''-digits" to select from. Consider the following: If a 3-digit number is squared, it can yield a 6-digit number (e.g. 540<sup>''2''</sup> = 291600). If there were to be middle 3 digits, 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 ''n''-digit number (e.g. 540 β 0540). For a generator of ''n''-digit numbers, the period can be no longer than 8<sup>''n''</sup>. 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 middle-squared 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|Python 3.12]]. <syntaxhighlight lang="python"> 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}.") </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)