[Python-checkins] CVS: python/dist/src/Lib random.py,1.18,1.19

Tim Peters tim_one@users.sourceforge.net
Thu, 25 Jan 2001 22:49:58 -0800


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

Modified Files:
	random.py 
Log Message:
SF bug 130030: Claim of bad betavariate algorithm.


Index: random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/random.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -r1.18 -r1.19
*** random.py	2001/01/25 20:25:57	1.18
--- random.py	2001/01/26 06:49:56	1.19
***************
*** 462,473 ****
  
  ## -------------------- beta --------------------
  
      def betavariate(self, alpha, beta):
! 
!         # Discrete Event Simulation in C, pp 87-88.
! 
!         y = self.expovariate(alpha)
!         z = self.expovariate(1.0/beta)
!         return z/(y+z)
  
  ## -------------------- Pareto --------------------
--- 462,486 ----
  
  ## -------------------- beta --------------------
+ ## See
+ ## http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470
+ ## for Ivan Frohne's insightful analysis of why the original implementation:
+ ##
+ ##    def betavariate(self, alpha, beta):
+ ##        # Discrete Event Simulation in C, pp 87-88.
+ ##
+ ##        y = self.expovariate(alpha)
+ ##        z = self.expovariate(1.0/beta)
+ ##        return z/(y+z)
+ ##
+ ## was dead wrong, and how it probably got that way.
  
      def betavariate(self, alpha, beta):
!         # This version due to Janne Sinkkonen, and matches all the std
!         # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
!         y = self.gammavariate(alpha, 1.)
!         if y == 0:
!             return 0.0
!         else:
!             return y / (y + self.gammavariate(beta, 1.))
  
  ## -------------------- Pareto --------------------