Search code examples
pythonpython-2.7randomseeding

Is there a maximum character limit to random seed?


Is there a maximum number of characters (and therfore value) to the seed in Python?

import random
random.seed(13) # fine
random.seed(1234567890) # also fine
random.seed(31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989)
# also fine

I'm not sure why someone would pick such a high value, but I just want to know if has a limit.


Solution

  • There is no max limit, but the input is eventually truncated to 20,000 bits.

    Even if you don't understand the algorithm (I don't), you can follow along in the source code.

    First, CPython splits the input into 32-bit chunks, and creates a bytearray out of them: https://github.com/python/cpython/blob/55edd0c185ad2d895b5d73e47d67049bc156b654/Modules/_randommodule.c#L258-L293

    Then, the Mersene Twister PRNG is seeded with the number 19,650,218, after which some mathematical stuff happens that is well above my understanding: https://github.com/python/cpython/blob/55edd0c185ad2d895b5d73e47d67049bc156b654/Modules/_randommodule.c#L172-L196

    But in the midst of all that action, notice this line: https://github.com/python/cpython/blob/55edd0c185ad2d895b5d73e47d67049bc156b654/Modules/_randommodule.c#L180

    Anything after the Nth element is never looped over, and therefore effectively discarded. In this case is N is 624 (it's a magic constant: https://github.com/python/cpython/blob/55edd0c185ad2d895b5d73e47d67049bc156b654/Modules/_randommodule.c#L76), so no more than 625 * 32 = 20,000 input bytes are used.

    Since the comment in the code says "from the right", we will take them at their word (this is all pretty far above my head, so I'm in no position to question them). Therefore I feel confident enough to conclude that the rightmost 20,000 bits of the input are used, and the rest discarded.

    That said, I imagine the definition of "rightmost" is platform-dependent, so it's probably not behavior you should rely on.