Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

random

julia

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.

like image 236
newtothis Avatar asked Oct 19 '21 06:10

newtothis


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
like image 62
DNF Avatar answered Oct 07 '22 12:10

DNF