# Calculating periodicity of a pseudo random number generator (Middle-square method) [closed]

Below is a Python code that calculates the periodicity of the middle-square method for a given 4 digit-numbers:

n = int(input("Please enter a four digit number: "))
n = int(str(n * n).zfill(8)[2:6])
print(n)


For example the seed number 9267 yield to a series that enters in a short loop of periodicity 4 [9267, …, 6100, 2100, 4100, 8100, 6100, 2100, 4100, 8100, 6100, ...]

Is there any solution to find out what numbers yield to the longest periodicity without having to simulate? If no, why isn't there a solution? Generally speaking, are there solutions to find out the periodicity of a given seed in different random number generators?

• Welcome to SciComp! If I understand your question correctly, you are asking about theoretical properties of a certain random number generator, and not its implementation or use in a computational algorithm? Jul 24 '14 at 7:10
• @ChristianClason Yes, that's correct. Is it off-topic? I didn't dare to ask my question on CSTheory.SE as this site is made for research level question and I am not not at all a computer scientist. Jul 24 '14 at 7:23
• In this case, I would say it is indeed off-topic. You are probably right about CSTheory -- I would rather try math.stackexchange.com (since you can use number theory to analyze RNGs) or crypto.stackexchange.com (since the analysis of RNGs, especially their periodicity, is a central topic in cryptography since that is information that can be used to break a scheme). You can flag your question and ask for migration (under "other"). Jul 24 '14 at 7:31
• On the other hand, a question about which random number generator to use for a particular purpose in scientific computing would be on-topic. What do you need the random numbers for, and why do you care about periodicity? Jul 24 '14 at 7:33
• @GeoffOxberry This migration to Cryptography was rejected because this isn't about a crypto RNG. (Yes, our help center could be read to imply that any RNG is on-topic. I've requested to it less misleading.) This question could fit on Computer Science, or probably even Mathematics. Jul 24 '14 at 20:46