Skip to content

function to choose element at random from an array #3075

Closed

Description

I think a useful utility function is to chose a random element from an array. Python has this in random.choice. Here is what a Julia implementation might look like:

function choice(a::Array)
    n = length(a)
    idx = mod(rand(Uint),n)+1
    return a[idx]
end

An option would be to have an additional argument for drawing some number of samples. This would be sampling with replacement from an array with uniform probability.

Another useful function could be to have sampling without replacement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions