Issue #13355: Raise ValueError on random.triangular call with invalid params.

Initial patch by Yuriy Senko.
This commit is contained in:
Andrew Svetlov 2013-04-12 23:27:37 +03:00
commit 730001163f
4 changed files with 42 additions and 2 deletions

View file

@ -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: