[Python-checkins] python/dist/src/Lib random.py,1.49,1.50

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Wed, 18 Jun 2003 20:46:48 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv31462/Lib

Modified Files:
	random.py 
Log Message:
randrange():  Repaired my overly optimistic rewrite, and added comments
explaining what's wrong with the two simpler variants.


Index: random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/random.py,v
retrieving revision 1.49
retrieving revision 1.50
diff -C2 -d -r1.49 -r1.50
*** random.py	19 Jun 2003 03:23:06 -0000	1.49
--- random.py	19 Jun 2003 03:46:46 -0000	1.50
***************
*** 149,153 ****
              raise ValueError, "non-integer stop for randrange()"
          if step == 1 and istart < istop:
!             return int(istart + self.random()*(istop - istart))
          if step == 1:
              raise ValueError, "empty range for randrange()"
--- 149,165 ----
              raise ValueError, "non-integer stop for randrange()"
          if step == 1 and istart < istop:
!             # Note that
!             #     int(istart + self.random()*(istop - istart))
!             # instead would be incorrect.  For example, consider istart
!             # = -2 and istop = 0.  Then the guts would be in
!             # -2.0 to 0.0 exclusive on both ends (ignoring that random()
!             # might return 0.0), and because int() truncates toward 0, the
!             # final result would be -1 or 0 (instead of -2 or -1).
!             #     istart + int(self.random()*(istop - istart))
!             # would also be incorrect, for a subtler reason:  the RHS
!             # can return a long, and then randrange() would also return
!             # a long, but we're supposed to return an int (for backward
!             # compatibility).
!             return int(istart + int(self.random()*(istop - istart)))
          if step == 1:
              raise ValueError, "empty range for randrange()"