[Python-checkins] cpython (3.4): Issue 13355: Make random.triangular degrade gracefully when low == high.

raymond.hettinger python-checkins at python.org
Mon May 26 02:28:46 CEST 2014


http://hg.python.org/cpython/rev/7ea6c8eb91e2
changeset:   90831:7ea6c8eb91e2
branch:      3.4
parent:      90829:d7eea8f608c2
user:        Raymond Hettinger <python at rcn.com>
date:        Sun May 25 17:25:27 2014 -0700
summary:
  Issue 13355:  Make random.triangular degrade gracefully when low == high.

files:
  Lib/random.py           |  5 ++++-
  Lib/test/test_random.py |  2 +-
  Misc/NEWS               |  3 +++
  3 files changed, 8 insertions(+), 2 deletions(-)


diff --git a/Lib/random.py b/Lib/random.py
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -355,7 +355,10 @@
 
         """
         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
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -602,7 +602,7 @@
         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),
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@
 - Issue #14710: pkgutil.find_loader() no longer raises an exception when a
   module doesn't exist.
 
+- Issue #13355: random.triangular() no longer fails with a ZeroDivisionError
+  when low equals high.
+
 - Issue #21538: The plistlib module now supports loading of binary plist files
   when reference or offset size is not a power of two.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list