diff -r f349e03bb41f Lib/random.py --- a/Lib/random.py Tue Mar 05 01:37:02 2013 -0500 +++ b/Lib/random.py Sun Apr 07 14:53:30 2013 +0300 @@ -342,6 +342,16 @@ 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: diff -r f349e03bb41f Lib/test/test_random.py --- a/Lib/test/test_random.py Tue Mar 05 01:37:02 2013 -0500 +++ b/Lib/test/test_random.py Sun Apr 07 14:53:30 2013 +0300 @@ -79,6 +79,28 @@ shuffle(lst) self.assertTrue(lst != shuffled_lst) + def test_triangular(self): + """Check that triangular() correctly handles bad input. See issue 13355. + """ + with self.assertRaises(ValueError): + # mode > high. + random.triangular(mode=2) + random.triangular(low=1, hihg=10, mode=11) + random.triangular(low=1, hihg=1, mode=11) + + # mode < low. + random.triangular(mode=-1) + random.triangular(low=1, high=10, mode=0) + random.triangular(low=1, high=1, mode=0) + + # low > high + random.triangular(low=5, high=2) + random.triangular(low=5, high=2, mode=1) + random.triangular(low=-2, high=-5) + + self.assertEqual(random.triangular(low=10, high=10), 10) + self.assertEqual(random.triangular(low=10, high=10, mode=10), 10) + def test_choice(self): choice = self.gen.choice with self.assertRaises(IndexError): @@ -522,7 +544,7 @@ 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),