whrandom seed question
Michael Hudson
mwh21 at cam.ac.uk
Tue Nov 23 18:06:31 EST 1999
kyriacou at umbc.edu (Stelios Kyriacou) writes:
> Hi,
>
> I tried today the random number generator in python
> and have the following question:
>
> Shouldn't the following 3 lines produce the same number?
>
> >>> whrandom.seed(0,0,0); print whrandom.random()
> 0.39613674715
> >>> whrandom.seed(0,0,0); print whrandom.random()
> 0.170188890936
> >>> whrandom.seed(0,0,0); print whrandom.random()
> 0.831213988577
> >>>
Use the source luke!
>From /usr/lib/python1.5/whrandom.py:
def seed(self, x = 0, y = 0, z = 0):
if not type(x) == type(y) == type(z) == type(0):
raise TypeError, 'seeds must be integers'
if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
raise ValueError, 'seeds must be in range(0, 256)'
if 0 == x == y == z:
# Initialize from current time
import time
t = long(time.time() * 256)
t = int((t&0xffffff) ^ (t>>24))
t, x = divmod(t, 256)
t, y = divmod(t, 256)
t, z = divmod(t, 256)
# Zero is a poor seed, so substitute 1
self._seed = (x or 1, y or 1, z or 1)
So
>>> whrandom.seed(1,1,1); print whrandom.random()
0.0169309061997
>>> whrandom.seed(1,1,1); print whrandom.random()
0.0169309061997
>>> whrandom.seed(1,1,1); print whrandom.random()
0.0169309061997
as expected.
> Am I missing something???
New one on me that! You'd think None would be a better default.
Regards,
Michael
More information about the Python-list
mailing list