summaryrefslogtreecommitdiff
path: root/Lib/random.py
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2013-04-12 23:39:33 +0300
committerAndrew Svetlov <andrew.svetlov@gmail.com>2013-04-12 23:39:33 +0300
commitb6cdae3db4ebcd34ba7039b2b1423f5ff4e235d6 (patch)
tree355eea5659d61ee73628b549c2c51c30aacdfc6f /Lib/random.py
parent0ba584c0235127ac462eeace2358386307751bab (diff)
downloadcpython-git-b6cdae3db4ebcd34ba7039b2b1423f5ff4e235d6.tar.gz
Issue #13355: Raise ValueError on random.triangular call with invalid params.
Initial patch by Yuriy Senko.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/random.py b/Lib/random.py
index af04ab207d..8560fb9a25 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -367,6 +367,16 @@ class Random(_random.Random):
http://en.wikipedia.org/wiki/Triangular_distribution
"""
+ # Sanity check. According to the doc low must be less or equal to
+ # high. And mode should be somewhere between these bounds.
+ if low > high:
+ raise ValueError('high cannot be less then low.')
+ if mode is not None and (mode < low or mode > high):
+ raise ValueError('mode must be between low and high.')
+
+ if high == low:
+ return low
+
u = self.random()
c = 0.5 if mode is None else (mode - low) / (high - low)
if u > c: