[New-bugs-announce] [issue32736] random.triangular yields unexpected distribution when args mixed
report at bugs.python.org
Wed Jan 31 16:39:14 EST 2018
New submission from Edward Preble <preble.edward at gmail.com>:
The random.triangular function produces an unexpected distribution result (instead of an error or warning message) when the order of the 3 arguments are mixed up.
Python notebook with demo of issue here:
Cases 1-4 are OK
1. random.triangular(low, high, mode) (Docs specified usage)
2. random.triangular(high, low, mode)
3. random.triangular(low, high)
4. random.triangular(high, low)
Incorrect argument input (e.g. numpy style) yields distributions that are NOT 3-value-triangular and the output is also from different ranges than expected:
Incorrect arguments (first one is numpy.random.triangular style)
6. random.triangular(low, mode, high)
7. random.triangular(high, mode, low)
Raising errors was discouraged in prior discussion (https://bugs.python.org/issue13355) due to back compatibility concerns. However, I would argue that output of an incorrect distribution without a warning is a problem that -should- be broken, even in old code.
A possible solution, that might not break the old code (I haven't looked at all the edge cases):
If 3 arguments provided, need to be sure the mode is arg3:
If arg1 < arg2 < arg3, this is definitely wrong since the mode is definitely in the middle (wrong position).
If arg1 > arg2 > arg3, this is definitely wrong since the mode is definitely in the middle (wrong position).
Those tests would not break the old use cases, since the signs of the tests switch between arg1/arg2/arg3:
low, high, mode (would be arg1 < arg2 > arg3)
high, low, mode (would be arg1 > arg2 < arg3)
Not positive how all the <=, >= combos work out with these tests.
components: Library (Lib)
title: random.triangular yields unexpected distribution when args mixed
versions: Python 2.7, Python 3.6
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce