The middle-square method is a method for generating pseudorandom numbers. In practice it is highly flawed for many practical purposes. Its period is usually very short and it possesses some severe weaknesses. Repeated enough times, the middle-square method will either begin repeatedly generating the same number or cycle to the previous number in a sequence and loop indefinitely.

middlesquare(seed, n)

Arguments

seed

initial starting value

n

number of random numbers desired

Details

Despite its weaknesses, the middle-square method is a good introductory example for how pseudorandom number generators work. This C++ code is based on the python code shared on Wikipedia (link). For more information check out the Wikipedia

Examples

random_numbers <- middlesquare(1234, 1000)
# Plot numbers to see that they are random
plot(random_numbers)