Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 978c6ab

Browse files
committed
Issue 13355: Make random.triangular degrade gracefully when low == high.
1 parent a2fc99e commit 978c6ab

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

Lib/random.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,10 @@ def triangular(self, low=0.0, high=1.0, mode=None):
355355
356356
"""
357357
u = self.random()
358-
c = 0.5 if mode is None else (mode - low) / (high - low)
358+
try:
359+
c = 0.5 if mode is None else (mode - low) / (high - low)
360+
except ZeroDivisionError:
361+
return low
359362
if u > c:
360363
u = 1.0 - u
361364
c = 1.0 - c

Lib/test/test_random.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ def test_constant(self):
602602
for variate, args, expected in [
603603
(g.uniform, (10.0, 10.0), 10.0),
604604
(g.triangular, (10.0, 10.0), 10.0),
605-
#(g.triangular, (10.0, 10.0, 10.0), 10.0),
605+
(g.triangular, (10.0, 10.0, 10.0), 10.0),
606606
(g.expovariate, (float('inf'),), 0.0),
607607
(g.vonmisesvariate, (3.0, float('inf')), 3.0),
608608
(g.gauss, (10.0, 0.0), 10.0),

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Library
2424
- Issue #14710: pkgutil.find_loader() no longer raises an exception when a
2525
module doesn't exist.
2626

27+
- Issue #13355: random.triangular() no longer fails with a ZeroDivisionError
28+
when low equals high.
29+
2730
- Issue #21538: The plistlib module now supports loading of binary plist files
2831
when reference or offset size is not a power of two.
2932

0 commit comments

Comments
 (0)