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.choices(seq, k) #62614

Closed
tiran opened this issue Jul 9, 2013 · 4 comments
Closed

random.choices(seq, k) #62614

tiran opened this issue Jul 9, 2013 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@tiran
Copy link
Member

tiran commented Jul 9, 2013

BPO 18414
Nosy @rhettinger, @mdickinson, @pitrou, @tiran
Files
  • random_choices.patch
  • 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 2013-07-11.10:23:14.156>
    created_at = <Date 2013-07-09.13:11:25.594>
    labels = ['type-feature', 'library']
    title = 'random.choices(seq, k)'
    updated_at = <Date 2013-07-11.10:23:14.155>
    user = 'https://github.com/tiran'

    bugs.python.org fields:

    activity = <Date 2013-07-11.10:23:14.155>
    actor = 'christian.heimes'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2013-07-11.10:23:14.156>
    closer = 'christian.heimes'
    components = ['Library (Lib)']
    creation = <Date 2013-07-09.13:11:25.594>
    creator = 'christian.heimes'
    dependencies = []
    files = ['30877']
    hgrepos = []
    issue_num = 18414
    keywords = ['patch']
    message_count = 4.0
    messages = ['192747', '192781', '192847', '192860']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'pitrou', 'christian.heimes']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue18414'
    versions = ['Python 3.4']

    @tiran
    Copy link
    Member Author

    tiran commented Jul 9, 2013

    The random module has no method to obvious way to select k elements from a sequence without enforcing unique values in the result set. It's a rather useful feature for salts, randomly generated passwords and other applications. See bpo-18405

    The patch implements random.choices(seq, k) -> list

    @tiran tiran added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jul 9, 2013
    @rhettinger rhettinger self-assigned this Jul 9, 2013
    @pitrou
    Copy link
    Member

    pitrou commented Jul 9, 2013

    How about [random.choice(seq) for i in range(k)]? Isn't it obvious enough?

    @rhettinger
    Copy link
    Contributor

    I concur with Antoine. A list comprehension is the standard, obvious idiom for making a list from repeated function calls.

    Putting sampling without replacement in its own method makes the operation more opaque than a plain list comprehension (which doesn't need documentation to be clear about how many calls are made, what the output type is, its relationship to random.choice, whether k can be larger than the population, etc).

    @tiran
    Copy link
    Member Author

    tiran commented Jul 11, 2013

    You have convinced me. It's not worth yet another API method.

    @tiran tiran closed this as completed Jul 11, 2013
    @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
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants