diff options
author | Andrew Svetlov <andrew.svetlov@gmail.com> | 2013-04-12 20:39:33 (GMT) |
---|---|---|
committer | Andrew Svetlov <andrew.svetlov@gmail.com> | 2013-04-12 20:39:33 (GMT) |
commit | b6cdae3db4ebcd34ba7039b2b1423f5ff4e235d6 (patch) | |
tree | 355eea5659d61ee73628b549c2c51c30aacdfc6f | |
parent | 0ba584c0235127ac462eeace2358386307751bab (diff) | |
download | cpython-b6cdae3db4ebcd34ba7039b2b1423f5ff4e235d6.zip cpython-b6cdae3db4ebcd34ba7039b2b1423f5ff4e235d6.tar.gz cpython-b6cdae3db4ebcd34ba7039b2b1423f5ff4e235d6.tar.bz2 |
Issue #13355: Raise ValueError on random.triangular call with invalid params.
Initial patch by Yuriy Senko.
-rw-r--r-- | Lib/random.py | 10 | ||||
-rw-r--r-- | Lib/test/test_random.py | 29 | ||||
-rw-r--r-- | Misc/ACKS | 1 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
4 files changed, 42 insertions, 1 deletions
diff --git a/Lib/random.py b/Lib/random.py index af04ab2..8560fb9 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: diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 3316415..75e1afc 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -43,6 +43,33 @@ class TestBasicOps(unittest.TestCase): self.assertRaises(TypeError, self.gen.seed, 1, 2) self.assertRaises(TypeError, type(self.gen), []) + def test_triangular(self): + # Check that triangular() correctly handles bad input. See issue 13355. + # mode > high. + with self.assertRaises(ValueError): + random.triangular(mode=2) + with self.assertRaises(ValueError): + random.triangular(low=1, high=10, mode=11) + with self.assertRaises(ValueError): + random.triangular(low=1, high=1, mode=11) + # mode < low. + with self.assertRaises(ValueError): + random.triangular(mode=-1) + with self.assertRaises(ValueError): + random.triangular(low=1, high=10, mode=0) + with self.assertRaises(ValueError): + random.triangular(low=1, high=1, mode=0) + # low > high + with self.assertRaises(ValueError): + random.triangular(low=5, high=2) + with self.assertRaises(ValueError): + random.triangular(low=5, high=2, mode=1) + with self.assertRaises(ValueError): + 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_jumpahead(self): self.gen.seed() state1 = self.gen.getstate() @@ -543,7 +570,7 @@ class TestDistributions(unittest.TestCase): 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), @@ -907,6 +907,7 @@ Nick Seidenman Žiga Seilnach Yury Selivanov Fred Sells +Yuriy Senko Jiwon Seo Joakim Sernbrant Roger Serwy @@ -22,6 +22,9 @@ Core and Builtins Library ------- +- Issue #13355: Raise ValueError on random.triangular call with invalid params. + Initial patch by Yuriy Senko. + - Issue #17666: Fix reading gzip files with an extra field. - Issue #13150, #17512: sysconfig no longer parses the Makefile and config.h |