diff --git a/Lib/random.py b/Lib/random.py index ca90e1459d9..49b0f149a5a 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -314,7 +314,7 @@ def sample(self, population, k): randbelow = self._randbelow n = len(population) if not 0 <= k <= n: - raise ValueError("Sample larger than population") + raise ValueError("Sample larger than population or is negative") result = [None] * k setsize = 21 # size of a small set minus size of an empty list if k > 5: diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index d2fa9d25518..e228104efd7 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -110,6 +110,7 @@ def test_sample(self): self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 # Exception raised if size of sample exceeds that of population self.assertRaises(ValueError, self.gen.sample, population, N+1) + self.assertRaises(ValueError, self.gen.sample, [], -1) def test_sample_distribution(self): # For the entire allowable range of 0 <= k <= N, validate that