[Patches] A bug fix to betavariate() in random.py

Guido van Rossum guido@python.org
Tue, 30 May 2000 10:08:57 -0500


> The betavariate() in random.py has nothing to do with beta-distributed
> random numbers. Here's something that works:
> 
>   def betavariate(alpha, beta):
> + 	# Devroye, L. (1986) Non-Uniform Random Variate Generation,
> + 	# theorem 4.1A (p. 430)
> + 	# Bug fix courtesy Janne Sinkkonen <janne@iki.fi>
>   
> ! 	y = gammavariate(alpha,1)
> ! 	z = gammavariate(beta,1)
>   	return z/(y+z)

I don't know anything about this kind of math (I guess I shouldn't
have copied the original code -- I don't remember from where).

I did a Google search and finally found something that seems similar,
in sather:

http://www.icsi.berkeley.edu/~sather/Documentation/Compiler/CompilerBrowser/lined-rnd.sa.gen.html

But it seems to do the following:

def beta(a, b):
    x1 = gamma(a)
    if x1 == 0.0: return 0.0
    else: return x1/(x1+gamma(b))

meaning it returns y/(y+z) where you return z/(y+z); it also tests for
zero where you don't.

Do you have the actual *text* of the Devroye algorithm that you quote?
I've seen it quoted an awful lot but haven't seen his description.

As long as we're going to copy from elsewhere, we might as well do due
diligence...

--Guido van Rossum (home page: http://www.python.org/~guido/)