Commit 320fbb31 authored by Raymond Hettinger's avatar Raymond Hettinger

Issue 28475: Improve error message for random.sample() with k < 0....

Issue 28475:  Improve error message for random.sample() with k < 0. (Contributed by Francisco Couzo).
parent 0de0a05e
...@@ -314,7 +314,7 @@ class Random(_random.Random): ...@@ -314,7 +314,7 @@ class Random(_random.Random):
randbelow = self._randbelow randbelow = self._randbelow
n = len(population) n = len(population)
if not 0 <= k <= n: if not 0 <= k <= n:
raise ValueError("Sample larger than population") raise ValueError("Sample larger than population or is negative")
result = [None] * k result = [None] * k
setsize = 21 # size of a small set minus size of an empty list setsize = 21 # size of a small set minus size of an empty list
if k > 5: if k > 5:
......
...@@ -110,6 +110,7 @@ class TestBasicOps: ...@@ -110,6 +110,7 @@ class TestBasicOps:
self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0
# Exception raised if size of sample exceeds that of population # 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, population, N+1)
self.assertRaises(ValueError, self.gen.sample, [], -1)
def test_sample_distribution(self): def test_sample_distribution(self):
# For the entire allowable range of 0 <= k <= N, validate that # For the entire allowable range of 0 <= k <= N, validate that
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment