[Python-checkins] cpython (merge 3.3 -> default): Issue #13355: Raise ValueError on random.triangular call with invalid params.

andrew.svetlov python-checkins at python.org
Fri Apr 12 22:31:15 CEST 2013


http://hg.python.org/cpython/rev/613eb432b152
changeset:   83278:613eb432b152
parent:      83276:2b89e9a6b482
parent:      83277:c40e36a49033
user:        Andrew Svetlov <andrew.svetlov at gmail.com>
date:        Fri Apr 12 23:27:37 2013 +0300
summary:
  Issue #13355: Raise ValueError on random.triangular call with invalid params.

Initial patch by Yuriy Senko.

files:
  Lib/random.py           |  10 +++++++++
  Lib/test/test_random.py |  30 +++++++++++++++++++++++++++-
  Misc/ACKS               |   1 +
  Misc/NEWS               |   3 ++
  4 files changed, 42 insertions(+), 2 deletions(-)


diff --git a/Lib/random.py b/Lib/random.py
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -345,6 +345,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 --git a/Lib/test/test_random.py b/Lib/test/test_random.py
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -48,6 +48,33 @@
         self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
         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)
+
     @unittest.mock.patch('random._urandom') # os.urandom
     def test_seed_when_randomness_source_not_found(self, urandom_mock):
         # Random.seed() uses time.time() when an operating system specific
@@ -73,7 +100,6 @@
         for (seq, shuffled_seq) in zip(seqs, shuffled_seqs):
             self.assertEqual(len(seq), len(shuffled_seq))
             self.assertEqual(set(seq), set(shuffled_seq))
-
         # The above tests all would pass if the shuffle was a
         # no-op. The following non-deterministic test covers that.  It
         # asserts that the shuffled sequence of 1000 distinct elements
@@ -597,7 +623,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),
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1110,6 +1110,7 @@
 Žiga Seilnacht
 Yury Selivanov
 Fred Sells
+Yuriy Senko
 Jiwon Seo
 Iñigo Serna
 Joakim Sernbrant
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,9 @@
 Library
 -------
 
+- Issue #13355: Raise ValueError on random.triangular call with invalid params.
+  Initial patch by Yuriy Senko.
+
 - Issue #16658: add missing return to HTTPConnection.send()
   Patch by Jeff Knupp.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list