Sampling from a list of integers without repetition

Given a vector of integers

a = collect(1:10)

I would like to randomly sample 5 integers with guaranteed non-repetition. As far as I know,

b = rand(a, 10)

does not guarantee that. This is easily demonstrated via

for i in 1:100
    println(rand(1:10, 8))
end

How can it be done? Thanks.

I found the answer on Slack: https://stackoverflow.com/questions/36047516/julia-generating-unique-random-integer-array

StatsBase.rand(using StatsBase
a = sample(1:100, 10, replace = false)

Given that rand has 173 functions in its dispatch table, that adding a replace argument` in several of them could prove very useful.

3 Likes

sample is the correct method (and terminology) here. rand is for random number generation, and if it guaranteed unique values then the sequence would no longer be random.

6 Likes

To add to @tbeason’s excellent answer: the technical term for this is sampling without replacement, while rand is IID (as approximated by a pseudorandom process).

Because of this, adding such an option to rand would not be the right place for this functionality. If you know the term, sample is also more discoverable.

tpThank you all!