diff options
author | Raymond Hettinger <python@rcn.com> | 2014-05-26 00:40:25 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2014-05-26 00:40:25 (GMT) |
commit | 92df7529cb6c863d0fcd3247829b24833f62e285 (patch) | |
tree | f1983c544736eaeda03ec426ba8000f65d011bbe | |
parent | 1f94efc6265c77e9b45058a88eb86f8d49c2c7c7 (diff) | |
download | cpython-92df7529cb6c863d0fcd3247829b24833f62e285.zip cpython-92df7529cb6c863d0fcd3247829b24833f62e285.tar.gz cpython-92df7529cb6c863d0fcd3247829b24833f62e285.tar.bz2 |
Issue 13355: Make random.triangular degrade gracefully when low == high.
-rw-r--r-- | Lib/random.py | 5 | ||||
-rw-r--r-- | Lib/test/test_random.py | 2 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 8 insertions, 2 deletions
diff --git a/Lib/random.py b/Lib/random.py index e89fae6..3f96a37 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -371,7 +371,10 @@ class Random(_random.Random): """ u = self.random() - c = 0.5 if mode is None else (mode - low) / (high - low) + try: + c = 0.5 if mode is None else (mode - low) / (high - low) + except ZeroDivisionError: + return low if u > c: u = 1.0 - u c = 1.0 - c diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index ba447ee..1a5a86b 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -541,7 +541,7 @@ class TestDistributions(unittest.TestCase): for variate, args, expected in [ (g.uniform, (10.0, 10.0), 10.0), (g.triangular, (10.0, 10.0), 10.0), - #(g.triangular, (10.0, 10.0, 10.0), 10.0), + (g.triangular, (10.0, 10.0, 10.0), 10.0), (g.expovariate, (float('inf'),), 0.0), (g.vonmisesvariate, (3.0, float('inf')), 3.0), (g.gauss, (10.0, 0.0), 10.0), @@ -23,6 +23,9 @@ Tests - Issue #19925: Added tests for the spwd module. Original patch by Vajrasky Kok. +- Issue #13355: random.triangular() no longer fails with a ZeroDivisionError + when low equals high. + - Issue #21522: Added Tkinter tests for Listbox.itemconfigure(), PanedWindow.paneconfigure(), and Menu.entryconfigure(). |