How to select n unique elements at random, from a list of elements in Julia

Viewed 601

I want to generate n unique elements from a list of numbers. I came across this answer but that only gives me one element. I want n distinct elements from the list.

How do I go about doing this?

I have tried using rand(list,n) but this sometimes gives me repeated elements from list so that doesn't work.

1 Answers

Try Distributions.sample StatsBase.sample:

jl> using StatsBase: sample

jl> x = rand(10);

jl> sample(x, 3; replace=false)
3-element Vector{Float64}:
 0.6816165016249632
 0.8500982926818003
 0.6518188633423712
Related