This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: random.Random.sample bug when counts is not None
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: jonfranco, miss-islington, rhettinger
Priority: normal Keywords: patch

Created on 2021-01-16 22:58 by jonfranco, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24235 merged jonfranco, 2021-01-17 11:16
PR 24243 merged miss-islington, 2021-01-18 18:05
Messages (6)
msg385152 - (view) Author: Jon FRANCO (jonfranco) * Date: 2021-01-16 22:58
Hello,

If I am reading right, random.Random.sample method has a bug if counts is not None.

Line 482 (of master):
"""
            selections = sample(range(total), k=k)
"""
this is calling the module function 'sample' and not the instance method.

IMO this line should be:
"""
            selections = self.sample(range(total), k=k)
"""

Python 3.9.1 (default, Dec 11 2020, 14:32:07) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from random import Random
>>> r = Random()
>>> r.seed('bug')
>>> r.sample(range(50), 5, counts=range(1,51))
[34, 39, 31, 42, 38]
>>> r.seed('bug')
>>> r.sample(range(50), 5, counts=range(1,51))
[39, 11, 3, 12, 29]  <====== this should be [34, 39, 31, 42, 38]
>>> r.seed('nobug')
>>> r.sample(range(50), 5)
[0, 30, 23, 17, 49]
>>> r.seed('nobug')
>>> r.sample(range(50), 5)
[0, 30, 23, 17, 49]

Regards,
Jon FRANCO
msg385153 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-01-17 03:03
Would you like to submit a PR for this?
msg385158 - (view) Author: Jon FRANCO (jonfranco) * Date: 2021-01-17 11:27
PR submitted. 
Let me know if I missed something, this is also my first PR.
Regards.
msg385209 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-01-18 18:04
New changeset f7b5bacd7a0b2084ce699eda6f6f4b1adfa16590 by jonanifranco in branch 'master':
bpo-42944 Fix Random.sample when counts is not None (GH-24235)
https://github.com/python/cpython/commit/f7b5bacd7a0b2084ce699eda6f6f4b1adfa16590
msg385215 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-01-18 18:36
New changeset a90539f5723a4c34430761be8cba97daa8474abf by Miss Islington (bot) in branch '3.9':
bpo-42944 Fix Random.sample when counts is not None (GH-24235) (GH-24243)
https://github.com/python/cpython/commit/a90539f5723a4c34430761be8cba97daa8474abf
msg385216 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-01-18 18:36
Thanks for the report and the PR.
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87110
2021-01-18 18:36:36rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg385216

stage: patch review -> resolved
2021-01-18 18:36:15rhettingersetmessages: + msg385215
2021-01-18 18:05:40miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request23065
2021-01-18 18:04:32rhettingersetmessages: + msg385209
2021-01-17 11:27:56jonfrancosetmessages: + msg385158
2021-01-17 11:16:39jonfrancosetkeywords: + patch
stage: patch review
pull_requests: + pull_request23058
2021-01-17 03:03:37rhettingersetmessages: + msg385153
2021-01-17 02:58:15rhettingersetassignee: rhettinger
2021-01-16 22:58:54jonfrancocreate