Commit 45421e1e authored by Raymond Hettinger's avatar Raymond Hettinger

Issue 13355: Make random.triangular degrade gracefully when low == high.

parent 92799fea
...@@ -371,7 +371,10 @@ class Random(_random.Random): ...@@ -371,7 +371,10 @@ class Random(_random.Random):
""" """
u = self.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: if u > c:
u = 1.0 - u u = 1.0 - u
c = 1.0 - c c = 1.0 - c
......
...@@ -541,7 +541,7 @@ class TestDistributions(unittest.TestCase): ...@@ -541,7 +541,7 @@ class TestDistributions(unittest.TestCase):
for variate, args, expected in [ for variate, args, expected in [
(g.uniform, (10.0, 10.0), 10.0), (g.uniform, (10.0, 10.0), 10.0),
(g.triangular, (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.expovariate, (float('inf'),), 0.0),
(g.vonmisesvariate, (3.0, float('inf')), 3.0), (g.vonmisesvariate, (3.0, float('inf')), 3.0),
(g.gauss, (10.0, 0.0), 10.0), (g.gauss, (10.0, 0.0), 10.0),
......
...@@ -23,6 +23,9 @@ Tests ...@@ -23,6 +23,9 @@ Tests
- Issue #19925: Added tests for the spwd module. Original patch by Vajrasky Kok. - 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(), - Issue #21522: Added Tkinter tests for Listbox.itemconfigure(),
PanedWindow.paneconfigure(), and Menu.entryconfigure(). PanedWindow.paneconfigure(), and Menu.entryconfigure().
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment