From: Raymond Hettinger Date: Mon, 26 May 2014 00:25:27 +0000 (-0700) Subject: Issue 13355: Make random.triangular degrade gracefully when low == high. X-Git-Tag: v3.4.2rc1~504 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=978c6abced7f129a66c39135139e60b3e0fa4e0b;p=python Issue 13355: Make random.triangular degrade gracefully when low == high. --- diff --git a/Lib/random.py b/Lib/random.py index 174e755a02..464292876b 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -355,7 +355,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 37a222d622..103d462c64 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -602,7 +602,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), diff --git a/Misc/NEWS b/Misc/NEWS index 05230daf24..950040b47a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -24,6 +24,9 @@ Library - 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.