summaryrefslogtreecommitdiffstats
path: root/Lib/random.py
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2013-04-12 20:27:37 (GMT)
committerAndrew Svetlov <andrew.svetlov@gmail.com>2013-04-12 20:27:37 (GMT)
commit730001163f1bd7469c17743e1846cf2761639191 (patch)
tree2a566bd9998cc2fc632b40fb84bb47af8c721efe /Lib/random.py
parent3042b5ebf4aee83957428870b86de394646b731c (diff)
parenta2dfc35a1341fc73a074cda9190bb99a821f8095 (diff)
downloadcpython-730001163f1bd7469c17743e1846cf2761639191.zip
cpython-730001163f1bd7469c17743e1846cf2761639191.tar.gz
cpython-730001163f1bd7469c17743e1846cf2761639191.tar.bz2
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 b183f56..2fa9887 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -345,6 +345,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: