Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf87126

Browse files
committed
Issue 28475: Improve error message for random.sample() with k < 0. (Contributed by Francisco Couzo).
1 parent 546ce65 commit bf87126

2 files changed

Lines changed: 2 additions & 1 deletion

File tree

Lib/random.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def sample(self, population, k):
314314
randbelow = self._randbelow
315315
n = len(population)
316316
if not 0 <= k <= n:
317-
raise ValueError("Sample larger than population")
317+
raise ValueError("Sample larger than population or is negative")
318318
result = [None] * k
319319
setsize = 21 # size of a small set minus size of an empty list
320320
if k > 5:

Lib/test/test_random.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def test_sample(self):
110110
self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0
111111
# Exception raised if size of sample exceeds that of population
112112
self.assertRaises(ValueError, self.gen.sample, population, N+1)
113+
self.assertRaises(ValueError, self.gen.sample, [], -1)
113114

114115
def test_sample_distribution(self):
115116
# For the entire allowable range of 0 <= k <= N, validate that

0 commit comments

Comments
 (0)