Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random.triangular yields unexpected distribution when args mixed #76917

Closed
epreble mannequin opened this issue Jan 31, 2018 · 2 comments
Closed

random.triangular yields unexpected distribution when args mixed #76917

epreble mannequin opened this issue Jan 31, 2018 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@epreble
Copy link
Mannequin

epreble mannequin commented Jan 31, 2018

BPO 32736
Nosy @rhettinger, @epreble

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-01-31.23:51:24.835>
created_at = <Date 2018-01-31.21:39:14.146>
labels = ['type-bug', 'library']
title = 'random.triangular yields unexpected distribution when args mixed'
updated_at = <Date 2018-01-31.23:51:24.814>
user = 'https://github.com/epreble'

bugs.python.org fields:

activity = <Date 2018-01-31.23:51:24.814>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2018-01-31.23:51:24.835>
closer = 'rhettinger'
components = ['Library (Lib)']
creation = <Date 2018-01-31.21:39:14.146>
creator = 'eddieprebs'
dependencies = []
files = []
hgrepos = []
issue_num = 32736
keywords = []
message_count = 2.0
messages = ['311377', '311391']
nosy_count = 2.0
nosy_names = ['rhettinger', 'eddieprebs']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32736'
versions = ['Python 2.7', 'Python 3.6']

@epreble
Copy link
Mannequin Author

epreble mannequin commented Jan 31, 2018

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:
https://github.com/epreble/random_triangular_distribution_issue

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)
or:
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.

@epreble epreble mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 31, 2018
@rhettinger
Copy link
Contributor

Sorry, while appreciate the sentiment and how well you articulated it, I'm going to decline this one for the reasons listed in the original discussion at https://bugs.python.org/issue13355 and because it is important to keep this code fast and light (so that large numbers of random variates can be created). Also, for your own code, it is trivially easy to wrap the existing function with your own error checking. Lastly, the ship for 2.7 sailed many years ago.

Note, almost note of the random functions can defend themselves are incorrect argument order. And who knows, there may be legitimate use cases for having the midpoint not between the low and high.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant