Skip to content
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

random.Random.sample bug when counts is not None #87110

Closed
jonanifranco mannequin opened this issue Jan 16, 2021 · 6 comments
Closed

random.Random.sample bug when counts is not None #87110

jonanifranco mannequin opened this issue Jan 16, 2021 · 6 comments
Assignees
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jonanifranco
Copy link
Mannequin

jonanifranco mannequin commented Jan 16, 2021

BPO 42944
Nosy @rhettinger, @miss-islington, @jonanifranco
PRs
  • bpo-42944 Fix Random.sample when counts is not None #24235
  • [3.9] bpo-42944 Fix Random.sample when counts is not None (GH-24235) #24243
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2021-01-18.18:36:36.628>
    created_at = <Date 2021-01-16.22:58:54.655>
    labels = ['type-bug', 'library', '3.9', '3.10']
    title = 'random.Random.sample bug when counts is not None'
    updated_at = <Date 2021-01-18.18:36:36.628>
    user = 'https://github.com/jonanifranco'

    bugs.python.org fields:

    activity = <Date 2021-01-18.18:36:36.628>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2021-01-18.18:36:36.628>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2021-01-16.22:58:54.655>
    creator = 'jonfranco'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42944
    keywords = ['patch']
    message_count = 6.0
    messages = ['385152', '385153', '385158', '385209', '385215', '385216']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'miss-islington', 'jonfranco']
    pr_nums = ['24235', '24243']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue42944'
    versions = ['Python 3.9', 'Python 3.10']

    @jonanifranco
    Copy link
    Mannequin Author

    jonanifranco mannequin commented Jan 16, 2021

    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

    @jonanifranco jonanifranco mannequin added 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 16, 2021
    @rhettinger
    Copy link
    Contributor

    Would you like to submit a PR for this?

    @jonanifranco
    Copy link
    Mannequin Author

    jonanifranco mannequin commented Jan 17, 2021

    PR submitted.
    Let me know if I missed something, this is also my first PR.
    Regards.

    @rhettinger
    Copy link
    Contributor

    New changeset f7b5bac by jonanifranco in branch 'master':
    bpo-42944 Fix Random.sample when counts is not None (GH-24235)
    f7b5bac

    @rhettinger
    Copy link
    Contributor

    New changeset a90539f by Miss Islington (bot) in branch '3.9':
    bpo-42944 Fix Random.sample when counts is not None (GH-24235) (GH-24243)
    a90539f

    @rhettinger
    Copy link
    Contributor

    Thanks for the report and the PR.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant