Skip to content

choose_multiple should return Result or Option #1620

Open
@serhii-havrylov

Description

@serhii-havrylov

I mistakenly assumed that choose_multiple functions like Python's random.choices, which performs sampling with replacement. This led to unexpected behaviour in the following Rust code:

let sample =  5..20.choose_multiple(&mut rng, my_vec.len())

Here, my_vec contains significantly more than 15 elements, but sample ends up with only 15 elements. This behaviour can be surprising and may introduce bugs.

Since choose_multiple performs sampling without replacement, it would make more sense for it to return a Result or Option to return error or None when the requested number of elements cannot be selected without replacement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    X-bugType: bug report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions