diff options
author | Raymond Hettinger <python@rcn.com> | 2017-01-07 00:13:37 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2017-01-07 00:13:37 (GMT) |
commit | 9b7ae96b98238627751e2f508ba5d600363555ef (patch) | |
tree | 47df1395ee5030abc3968634bd88240691e13500 | |
parent | 9220111fc5e2ed6503fc3260e09faeec5101c7d8 (diff) | |
download | cpython-9b7ae96b98238627751e2f508ba5d600363555ef.zip cpython-9b7ae96b98238627751e2f508ba5d600363555ef.tar.gz cpython-9b7ae96b98238627751e2f508ba5d600363555ef.tar.bz2 |
Issue #29023: Clarify that ints and longs are always deterministic seeds for random.
-rw-r--r-- | Doc/library/random.rst | 18 | ||||
-rw-r--r-- | Lib/random.py | 6 |
2 files changed, 13 insertions, 11 deletions
diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 2a3c3af..852fe7c 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -71,17 +71,17 @@ from sources provided by the operating system. Bookkeeping functions: -.. function:: seed([x]) +.. function:: seed(a=None) - Initialize the basic random number generator. Optional argument *x* can be any - :term:`hashable` object. If *x* is omitted or ``None``, current system time is used; - current system time is also used to initialize the generator when the module is - first imported. If randomness sources are provided by the operating system, - they are used instead of the system time (see the :func:`os.urandom` function - for details on availability). + Initialize internal state of the random number generator. - If a :term:`hashable` object is given, deterministic results are only assured - when :envvar:`PYTHONHASHSEED` is disabled. + ``None`` or no argument seeds from current time or from an operating + system specific randomness source if available (see the :func:`os.urandom` + function for details on availability). + + If *a* is not ``None`` or an :class:`int` or a :class:`long`, then + ``hash(a)`` is used instead. Note that the hash values for some types + are nondeterministic when :envvar:`PYTHONHASHSEED` is enabled. .. versionchanged:: 2.4 formerly, operating system resources were not used. diff --git a/Lib/random.py b/Lib/random.py index 3f96a37..11fd35b 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -98,12 +98,14 @@ class Random(_random.Random): self.gauss_next = None def seed(self, a=None): - """Initialize internal state from hashable object. + """Initialize internal state of the random number generator. None or no argument seeds from current time or from an operating system specific randomness source if available. - If a is not None or an int or long, hash(a) is used instead. + If a is not None or is an int or long, hash(a) is used instead. + Hash values for some types are nondeterministic when the + PYTHONHASHSEED environment variable is enabled. """ if a is None: |