[ python-Bugs-901605 ] random.seed not initialized as advertised
SourceForge.net
noreply at sourceforge.net
Sat Feb 21 02:46:15 EST 2004
Bugs item #901605, was opened at 2004-02-21 00:45
Message generated for change (Comment added) made by rhettinger
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=901605&group_id=5470
Category: Python Library
Group: Python 2.2.2
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: paul rubin (phr)
>Assigned to: Raymond Hettinger (rhettinger)
Summary: random.seed not initialized as advertised
Initial Comment:
The doc for random.seed(x) says: Optional argument x
can be any 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.
In fact the random module calls the seed function with
x=None when a Random object is instantiated, not with
the current system time as the docs indicate. You can
tell this by doing something like:
import random
class myrand(random.Random):
def seed(x):
print x
random.Random.seed(x)
a = myrand()
and you'll see it prints None. Initializing with None
may be reasonable behavior, but it doesn't match the
docs. So either the code or the doc should be updated.
----------------------------------------------------------------------
>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-02-21 02:46
Message:
Logged In: YES
user_id=80475
I believe you are misreading the docs. They describe what
Random.seed() DOES with the arguments, not HOW it is called.
The code for seed() shows that if the argument is None, it
calls time.time() and uses that as the seed value:
"""
if a is None:
# Initialize from current time
import time
a = long(time.time() * 256)
if type(a) not in (type(3), type(3L)):
a = hash(a)
a, x = divmod(a, 30268)
a, y = divmod(a, 30306)
a, z = divmod(a, 30322)
self._seed = int(x)+1, int(y)+1, int(z)+1
self.gauss_next = None
"""
I understand that this behavior (which DOES match the
documentation) may be inconvenient if you override the
seed() method. You are pretty much forced to add a few
lines of code to replicate seed's documented behavior.
If the module were being rewritten from scratch, this
behavior would be factored out. However, it has been this
way since the dawn of time and is not sufficiently painful
to warrant a change. Or, more accurately put, changing it
would cause more discomfort than it would save (i.e. the
huge existing code base takes priority here).
----------------------------------------------------------------------
Comment By: Tim Peters (tim_one)
Date: 2004-02-21 02:39
Message:
Logged In: YES
user_id=31435
The docs are correct. They say that calling seed() with no
argument is the same as calling seed(None), and that's true,
and it's also true that both cases end up using current
system time. That happens because the implementation of
seed() special-cases argument None, but the docs describe
behavior, not implementation details.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=901605&group_id=5470
More information about the Python-bugs-list
mailing list