]> granicus.if.org Git - python/commitdiff
Issue 13355: Make random.triangular degrade gracefully when low == high.
authorRaymond Hettinger <python@rcn.com>
Mon, 26 May 2014 00:40:25 +0000 (17:40 -0700)
committerRaymond Hettinger <python@rcn.com>
Mon, 26 May 2014 00:40:25 +0000 (17:40 -0700)
Lib/random.py
Lib/test/test_random.py
Misc/NEWS

index e89fae663a7b538bf2ed652f5aa299543e4cb081..3f96a3770b4e8d517fc1081abb3dc998cd2fd614 100644 (file)
@@ -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
index ba447ee57aa50657a672e3cc83459969f655cd19..1a5a86b9e23ca212ba73480f23e37f811e8f92f1 100644 (file)
@@ -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),
index d46d1d30636c4997d5905b544fb0eff79bd09a4b..04e9221d944058931dd4870ae17b5c00c3ab4b65 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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().