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.

Author terry.reedy
Recipients mark.dickinson, mark108, rhettinger, terry.reedy
Date 2011-11-13.04:11:44
SpamBayes Score 4.1727732e-13
Marked as misclassified No
Message-id <1321157505.37.0.920741069522.issue13355@psf.upfronthosting.co.za>
In-reply-to
Content
3.2 doc entry:

random.triangular(low, high, mode) 
Return a random floating point number N such that low <= N <= high and with the specified mode between those bounds. The low and high bounds default to zero and one. The mode argument defaults to the midpoint between the bounds, giving a symmetric distribution.

3.2 behavior:
>>> from random import triangular
>>> triangular(1,1)
1.0
>>> triangular(1,1,1)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    triangular(1,1,1)
  File "C:\Programs\Python32\lib\random.py", line 346, in triangular
    c = 0.5 if mode is None else (mode - low) / (high - low)
ZeroDivisionError: division by zero

I regard is as a bug that explicitly giving a 'default value' causes the function to fail. 

The last sentence of the doc is a lie: the actual default for mode is None: 

>>> triangular(1,1,None)
1.0

and if it is None, it *not* calculated (low + .5(high-low)). The actual internal third parameter is the fraction of the range (high-low) that is the up slope versus the down slope of the distribution. The code calls that 'c', as calculated by the line shown in the traceback. The fix is simple: add 'or low==high' to the condition.

    c = 0.5 if (mode is None or low==high) else (mode - low) / (high - low)

Contrary to the doc ('mode between those bounds'), the definition on Wikipedia and code include the degenerate cases of mode == low or high. The code in effect treats modes outside the range as being at an endpoint.

Suggested doc revision, with defaults given in the signature as normal:

random.triangular(low=0.0, high=1.0, mode=None) 
Return a random floating point number N from a triangular distribution such that low <= N <= high with the specified mode between or at those bounds. A mode outside a bound is treated as being at the bound. The default mode argument corresponds to the midpoint between the bounds, giving a symmetric distribution.
History
Date User Action Args
2011-11-13 04:11:45terry.reedysetrecipients: + terry.reedy, rhettinger, mark.dickinson, mark108
2011-11-13 04:11:45terry.reedysetmessageid: <1321157505.37.0.920741069522.issue13355@psf.upfronthosting.co.za>
2011-11-13 04:11:44terry.reedylinkissue13355 messages
2011-11-13 04:11:44terry.reedycreate