vendredi 8 janvier 2021

Randomly picking unique elements

I use rand to reach random three element from a and add this m values to array but I want them to be unique. So, array can't be like this: array = [1,1,2]. How can I check when two elements are equal and how to prevent this other than sample method? I was thinking about this: Let's assume m=1 when times method runs the first time. If m =1 at the second time, I want to skip this value and reach a different one. Is there any code explanation for this ? Or maybe more different way?

a = [1, 2, 3, 4]
array = []

3.times do 
  m = a[rand(a.size)]
  array << m 
end



Aucun commentaire:

Enregistrer un commentaire