2

In Python 2 documentation of the random.seed() function I found a warning:

If a hashable object is given, deterministic results are only assured when PYTHONHASHSEED is disabled.

From https://docs.python.org/2/using/cmdline.html#envvar-PYTHONHASHSEED I infer that the -R switch of the interpreter may have similar effect as PYTHONHASHSEED.

I have verified empirically, that random numbers seeded with small integers seems to be reproducible. So do hashes of small integers.

However, int is hashable. Is it stated explicitly in any trusted source, that it is safe to use it as a seed for a reproducible sequence of random numbers?

In contrast to Reproducibility of python pseudo-random numbers across systems and versions?, reproducibility within same system and interpreter is enough.

1
  • I would consider that a documentation flaw; it should say that ints and longs are not hashed. In any case, the Python 2 implementation is unlikely to change at this point, and the Python 3 documentation mentions that ints (Python 2 longs) are used directly. Commented Dec 19, 2016 at 17:31

2 Answers 2

3

Not a complete answer but the source code for random_seed (in C) would be relevent:

if (PyInt_Check(arg) || PyLong_Check(arg))
    n = PyNumber_Absolute(arg);
else {
    long hash = PyObject_Hash(arg);
    if (hash == -1)
        goto Done;
    n = PyLong_FromUnsignedLong((unsigned long)hash);
}

this would suggest that anything other then a long (int) directly uses the hash value as the seed, so as long as:

  1. hash(int) gives consistent results and
  2. You are using this implementation of seed (may not be the same for Jython etc.)

Then I'd expect seed(int) to yield consistent results.

That said I can't speak for either of those conditions staying constant so this doesn't really give a definitive answer unless someone else can verify them.

Sign up to request clarification or add additional context in comments.

3 Comments

That looks like the Python 3 source code. The Python 2 code would be more relevant.
@user2357112 ok but other then checking if it is an int or long instead of just long the relevant parts are pretty much the same no?
@TadhgMcDonald-Jensen I agree with user2357112. It is not about int/long difference. The question was about Python 2 specifically, so Python 2.7 source code seems more appropriate
0

The documentation confirms its safety in Python 2.6:

If x is not None or an int or long, hash(x) is used instead. If x is an int or long, x is used directly.

(from https://docs.python.org/2.6/library/random.html#random.seed)

[EDIT]

The documentation for 2.7 has been updated to:

If a is not None or an int or a long, then hash(a) is used instead. Note that the hash values for some types are nondeterministic when PYTHONHASHSEED is enabled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.