Content deleted Content added
Overitbynow (talk | contribs) Link suggestions feature: 2 links added. |
|||
(48 intermediate revisions by 25 users not shown) | |||
Line 1:
{{Short description|Pseudorandom number generator}}
[[Image:
[[Image:Middle_square_method_2_digits.svg|right|250px|thumb|[[Directed graph]] of all 100 2-digit pseudorandom numbers obtained using the middle-square method with ''n'' = 2.]]
In [[mathematics]] and [[computer science]], the '''middle-square method''' is a method of generating [[pseudorandom number]]s. In practice it is
== History ==
===In mathematics===
The method
In the 1949 talk, Von Neumann quipped that
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.<ref name="Ekeland1996">{{cite book |author=Ivar Ekeland |title=The Broken Dice, and Other Mathematical Tales of Chance |date=15 June 1996 |publisher=University of Chicago Press |isbn=978-0-226-19992-4}}</ref> Supposedly, the manuscript is now lost, but [[Jorge Luis Borges]] sent Ekeland
▲method was invented by a Franciscan friar known only as Brother Edvin sometime between 1240 and 1250.<ref name="Ekeland1996">{{cite book|author=Ivar Ekeland|title=The Broken Dice, and Other Mathematical Tales of Chance|date=15 June 1996|publisher=University of Chicago Press|isbn=978-0-226-19992-4}}</ref> 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 ''n''-digit pseudorandom numbers, an ''n''-digit starting value is created and squared, producing a
The value of ''n'' must be even in order for the method to work
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''
=== Example implementation ===
Here, the algorithm is rendered in [[Python 3|Python 3.12]].
<syntaxhighlight lang="python">
seed_number = int(input("Please enter a four
number = seed_number
already_seen = set()
Line 35 ⟶ 37:
print(f"#{counter}: {number}")
print(f"We began with {seed_number}
f" have repeated ourselves after {counter} steps"
f" with {number}.")
</syntaxhighlight>
==
* [[Linear congruential generator]]▼
* [[Blum Blum Shub]]▼
▲In the book "Random Numbers and Computers”<ref>{{cite book | title = Random Numbers and Computers | last = Kneusel | first = Ron | publisher = Springer | year = 2018 | edition = 1 | pages = 13-14 }}</ref>, Kneusel states the following:
* [[Hash function#Mid-squares | middle-square hash function ]]
==References==
<references/>
▲* [[Linear congruential generator]]
▲* [[Blum Blum Shub]]
{{DEFAULTSORT:Middle-Square Method}}
Line 100 ⟶ 54:
[[Category:Articles with example C code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:John von Neumann]]
|