Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-40541: Add optional *counts* parameter to random.sample() #19970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Earlier test for zero total weight
  • Loading branch information
rhettinger committed May 7, 2020
commit 556582d25bacb12b5a7c013a898679a51d88c625
2 changes: 1 addition & 1 deletion 2 Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def sample(self, population, k, *, weights=None):
total = cum_weights.pop()
if not isinstance(total, int):
raise TypeError('Weights must be integers')
if total < 0:
if total <= 0:
raise ValueError('Total of weights must be greater than zero')
selections = sample(range(total), k=k)
bisect = _bisect
Expand Down
2 changes: 2 additions & 0 deletions 2 Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ def test_sample_with_weights(self):
sample(['red', 'green', 'blue'], weights=10, k=10) # weights not iterable
with self.assertRaises(ValueError):
sample(['red', 'green', 'blue'], weights=[-3, -7, -8], k=2) # weights are negative
with self.assertRaises(ValueError):
sample(['red', 'green', 'blue'], weights=[0, 0, 0], k=2) # weights are zero
with self.assertRaises(ValueError):
sample(['red', 'green'], weights=[10, 10], k=21) # population too small
with self.assertRaises(ValueError):
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.