Reputation: 525
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.
Upvotes: 9
Views: 1959
Reputation: 12664
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
Upvotes: 15