Terry J. Reedy <tjre...@udel.edu> added the comment: 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. ---------- components: +Library (Lib) -Extension Modules keywords: +patch nosy: +terry.reedy stage: -> needs patch title: random.triangular error when low = mode -> random.triangular error when low = high=mode type: -> behavior versions: +Python 2.7, Python 3.2, Python 3.3 -Python 2.6 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue13355> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com