[Python-checkins] CVS: python/dist/src/Lib random.py,1.21,1.22
Tim Peters
tim_one@users.sourceforge.net
Wed, 31 Jan 2001 20:59:20 -0800
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv14254/python/dist/src/Lib
Modified Files:
random.py
Log Message:
Change random.seed() so that it can get at the full range of possible
internal states. Put the old .seed() (which could only get at about
the square root of the # of possibilities) under the new name .whseed(),
for bit-level compatibility with older versions. This occurred to me
while reviewing effbot's book (he found himself stumbling over .seed()
more than once there ...).
Index: random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/random.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -r1.21 -r1.22
*** random.py 2001/01/26 22:56:56 1.21
--- random.py 2001/02/01 04:59:18 1.22
***************
*** 67,74 ****
>>> g = Random(42) # arbitrary
>>> g.random()
! 0.24855401895528142
>>> g.jumpahead(6953607871644L - 1) # move *back* one
>>> g.random()
! 0.24855401895528142
"""
# XXX The docstring sucks.
--- 67,74 ----
>>> g = Random(42) # arbitrary
>>> g.random()
! 0.25420336316883324
>>> g.jumpahead(6953607871644L - 1) # move *back* one
>>> g.random()
! 0.25420336316883324
"""
# XXX The docstring sucks.
***************
*** 120,143 ****
# getstate(), setstate() and jumpahead() methods.
! def __whseed(self, x=0, y=0, z=0):
! """Set the Wichmann-Hill seed from (x, y, z).
! These must be integers in the range [0, 256).
"""
! 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)
def random(self):
--- 120,148 ----
# getstate(), setstate() and jumpahead() methods.
! def seed(self, a=None):
! """Initialize internal state from hashable object.
! None or no argument seeds from current time.
!
! If a is not None or an int or long, hash(a) is instead.
!
! If a is an int or long, a is used directly. Distinct values between
! 0 and 27814431486575L inclusive are guaranteed to yield distinct
! internal states (this guarantee is specific to the default
! Wichmann-Hill generator).
"""
! 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
def random(self):
***************
*** 172,195 ****
return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
- def seed(self, a=None):
- """Seed from hashable object's hash code.
-
- None or no argument seeds from current time. It is not guaranteed
- that objects with distinct hash codes lead to distinct internal
- states.
- """
-
- if a is None:
- self.__whseed()
- return
- a = hash(a)
- a, x = divmod(a, 256)
- a, y = divmod(a, 256)
- a, z = divmod(a, 256)
- x = (x + a) % 256 or 1
- y = (y + a) % 256 or 1
- z = (z + a) % 256 or 1
- self.__whseed(x, y, z)
-
def getstate(self):
"""Return internal state; can be passed to setstate() later."""
--- 177,180 ----
***************
*** 228,231 ****
--- 213,260 ----
self._seed = x, y, z
+ def __whseed(self, x=0, y=0, z=0):
+ """Set the Wichmann-Hill seed from (x, y, z).
+
+ These must be integers in the range [0, 256).
+ """
+
+ 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)
+
+ def whseed(self, a=None):
+ """Seed from hashable object's hash code.
+
+ None or no argument seeds from current time. It is not guaranteed
+ that objects with distinct hash codes lead to distinct internal
+ states.
+
+ This is obsolete, provided for compatibility with the seed routine
+ used prior to Python 2.1. Use the .seed() method instead.
+ """
+
+ if a is None:
+ self.__whseed()
+ return
+ a = hash(a)
+ a, x = divmod(a, 256)
+ a, y = divmod(a, 256)
+ a, z = divmod(a, 256)
+ x = (x + a) % 256 or 1
+ y = (y + a) % 256 or 1
+ z = (z + a) % 256 or 1
+ self.__whseed(x, y, z)
+
## ---- Methods below this point do not need to be overridden when
## ---- subclassing for the purpose of using a different core generator.
***************
*** 624,627 ****
--- 653,657 ----
setstate = _inst.setstate
jumpahead = _inst.jumpahead
+ whseed = _inst.whseed
if __name__ == '__main__':