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
# also fine

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

Asked By: Ghoul Fool



Since random.seed expects None, int, or long, there is technically no defined limit for the seed, since long can be very large, depending on the available address space, which serves as a practical limit. There is also no indication of a checked limit in the function anyway.

Answered By: Arnav Borborah

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:

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:

But in the midst of all that action, notice this line:

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:, 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.

Answered By: shadowtalker

While if use the numpy provision of random number generation using the np.random.seed(x). The values of seed is limited to 0 and 2**32 – 1.

Answered By: Lavender
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.