Bug in the standard module random ?

John Machin sjmachin at lexicon.net
Thu Mar 7 18:59:55 EST 2002


Weet Vanniks <No at Spam.Please> wrote in message news:<3C8791B1.6BBC1981 at Spam.Please>...
> Hi,
> 
> The  gammavariate function of the standard module is documented as
> taking two parameters, the first one alpha is required to be > -1 and
> the second beta is required to be >0.
> However, examining the implementation, it seems that the requirement for
> alpha is to be >0.
> 
> In spite of this, I still have a problem since I called the gammavariate
> function with a parameter alpha equal to 0.2 and it fails logically on
> the following line:
> ainv=_sqrt(2.0 * alpha - 1.0)
> 
> Apparently, the implementation requires alpha to be > 0.5.
> Am i missing something or is it a bug?

Yes *and* yes :-)

Reading on further in random.py, one finds that stdgamma() uses the
ainv, bbb, and ccc etc arguments only if alpha > 1.0

Further, stdgamma() is called (inside random.py) only from the line
you quoted.

The module exposes both gammavariate() and stdgamma() --- stdgamma()
with those three extra args seems useless to me.

The documentation says there is a gamma() but doesn't mention
gammavariate() and stdgamma(). The exposed function shold be called
"gammavariate" (a) for consistency (b) to avoid the perceived need to
explain that this "gamma" is not the gamma function!!! As you have
noticed, the usable range of alpha is incorrectly documented also.

You should be able to fix this yourself; if not, let me know your real
e-mail address and I'll send you a fixed version (and submit it to
SF).

> 
> Best regards
> 
> Weet
> 
> 
> ## -------------------- gamma distribution --------------------
> 
>     def gammavariate(self, alpha, beta):
>         # beta times standard gamma
>         ainv = _sqrt(2.0 * alpha - 1.0)
>         return beta * self.stdgamma(alpha, ainv, alpha - LOG4, alpha +
> ainv)



More information about the Python-list mailing list