[Python-checkins] python/dist/src/Lib random.py,1.26.6.7,1.26.6.8

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Sat, 04 Jan 2003 01:30:35 -0800


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

Modified Files:
      Tag: release22-maint
	random.py 
Log Message:
Correct long standing bugs in the methods for random distributions.
The range of u=random() is [0,1), so log(u) and 1/x can fail.
Fix by setting u=1-random() or by reselecting for a usable value.



Index: random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/random.py,v
retrieving revision 1.26.6.7
retrieving revision 1.26.6.8
diff -C2 -d -r1.26.6.7 -r1.26.6.8
*** random.py	5 Oct 2002 14:42:52 -0000	1.26.6.7
--- random.py	4 Jan 2003 09:30:32 -0000	1.26.6.8
***************
*** 400,404 ****
          while 1:
              u1 = random()
!             u2 = random()
              z = NV_MAGICCONST*(u1-0.5)/u2
              zz = z*z/4.0
--- 400,404 ----
          while 1:
              u1 = random()
!             u2 = 1.0 - random()
              z = NV_MAGICCONST*(u1-0.5)/u2
              zz = z*z/4.0
***************
*** 536,540 ****
              while 1:
                  u1 = random()
!                 u2 = random()
                  v = _log(u1/(1.0-u1))/ainv
                  x = alpha*_exp(v)
--- 536,542 ----
              while 1:
                  u1 = random()
!                 if not 1e-7 < u1 < .9999999:
!                     continue
!                 u2 = 1.0 - random()
                  v = _log(u1/(1.0-u1))/ainv
                  x = alpha*_exp(v)
***************
*** 668,672 ****
          # Jain, pg. 495
  
!         u = self.random()
          return 1.0 / pow(u, 1.0/alpha)
  
--- 670,674 ----
          # Jain, pg. 495
  
!         u = 1.0 - self.random()
          return 1.0 / pow(u, 1.0/alpha)
  
***************
*** 681,685 ****
          # Jain, pg. 499; bug fix courtesy Bill Arms
  
!         u = self.random()
          return alpha * pow(-_log(u), 1.0/beta)
  
--- 683,687 ----
          # Jain, pg. 499; bug fix courtesy Bill Arms
  
!         u = 1.0 - self.random()
          return alpha * pow(-_log(u), 1.0/beta)