This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: random.expovariate(0.0)
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: kbriggs, mark.dickinson, rhettinger
Priority: normal Keywords:

Created on 2009-01-07 16:17 by kbriggs, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg79346 - (view) Author: Keith Briggs (kbriggs) Date: 2009-01-07 16:17
random.expovariate(lambd) should handle lambd=0 cleanly, and probably
return a FP infinity.

At the moment it gives:

ZeroDivisionError: float division
msg79348 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-01-07 17:06
An exponential distribution with parameter 0 isn't an exponential distribution any more.  On the real line, there isn't
even a limiting distribution as the parameter approaches 0.

Is there really any use for having expovariate degenerate this way?
It seems much more likely that a call to random.expovariate(0) is
caused by a bug somewhere, so should raise an exception.  Which is exactly 
what it does already.

The proposed change would also not be in keeping with the philosophy 
behind most of Python's mathematics, which is to raise exceptions rather 
than returning exceptional (nan, inf) results.  (Think of it as IEEE 754 
will the invalid, overflow and division-by-zero signals all being 
trapped.)
msg79349 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-01-07 17:15
I would suggest adding a note to the documentation stating clearly that 
the parameter should be nonzero.  The documentation is also unclear on 
whether a negative parameter value is permitted, although the current code 
works exactly the way that I'd expect for a negative parameter.

If a negative parameter is permitted then the last sentence of the 
docstring:

"""... Returned values range from 0 to positive infinity."""

is inaccurate, since values range from 0 to negative infinity in that 
case.  If it's not supposed to be permitted then perhaps the docstring 
should mention that the parameter should be positive?
msg79350 - (view) Author: Keith Briggs (kbriggs) Date: 2009-01-07 17:21
That's all true - the only thing in favour of my suggestion is that to
allow lambda=0 is sometimes useful in simulations, meaning that it's
infinite time to the next event, i.e. it never occurs.  A FP infinity
would do this without requiring a special case.

At least the documentation should state the restrictions on lambda.
msg79351 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-01-07 17:39
> to allow lambda=0 is sometimes useful in simulations, meaning that it's
> infinite time to the next event, i.e. it never occurs.

Thanks; that makes sense.  If 1./0. returned inf then expovariate would 
already do what you want.  But rightly or wrongly, Python just doesn't 
work that way.

Incidentally, if expovariate had been written to take the mean as its 
argument (instead of the reciprocal of the mean) then this would be easy:  
just provide a parameter of float('inf') and everything would work.  (And 
a parameter of 0 would give a point distribution at 0, which again seems 
perfectly sound.)
msg79354 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-01-07 17:54
Doc fixes in r68378.  Closing.
msg79363 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-01-07 19:10
I concur with Mark.
History
Date User Action Args
2022-04-11 14:56:43adminsetgithub: 49119
2009-01-07 19:10:35rhettingersetnosy: + rhettinger
messages: + msg79363
2009-01-07 17:54:52mark.dickinsonsetstatus: open -> closed
resolution: rejected
messages: + msg79354
2009-01-07 17:39:49mark.dickinsonsetmessages: + msg79351
2009-01-07 17:21:33kbriggssetmessages: + msg79350
2009-01-07 17:15:40mark.dickinsonsetmessages: + msg79349
2009-01-07 17:06:24mark.dickinsonsettype: enhancement
messages: + msg79348
nosy: + mark.dickinson
components: + Library (Lib), - Extension Modules
versions: + Python 3.1, Python 2.7, - Python 2.6
2009-01-07 16:17:00kbriggscreate