Sampling without replacement

rand(1:20, 3)

gives three random integers in [1:20] with replacement. I see there are some threads on github on this issue, but I am not sure what is the recommended way with Julia 0.5 to sample without replacement.

3 Likes

See the StatsBase package: http://juliastats.github.io/StatsBase.jl/stable/sampling.html#Sampling-API-1

4 Likes

Without using external packages you can use randperm and take the first three elements:

randperm(20)[1:3]

But this may not be very efficient, as it needs to create first an array of 20 elements.

5 Likes

You can use splice! to pick random elements and remove them from the collection:

function sample_wo_repl!(A,n)
    sample = Array{eltype(A)}(n)
    for i in 1:n
        sample[i] = splice!(A, rand(eachindex(A)))
    end
    return sample
end

There is also randsubseq in Base. This efficiently samples an array without replacement, but with a given probability (per element) rather than a given number of elements. e.g. randsubseq(1:20, 0.15) produces 3 elements on average.

1 Like

Update: randperm and randsubseq have been moved from Base to Random

It’s still probably better to use StatsBase to solve this problem.

(Commenting here because this thread is the first result when I google “Julia sample without replacement”)

6 Likes

Update: the StatsBase URL has changed (again): https://juliastats.org/StatsBase.jl/stable/sampling/#Sampling-API-1

I can’t edit the above post anymore (when I click edit it only shows the history) - seems there is an editing limit?