b
is the maximum winner that I want.
b.times do
winner = participant[rand(participant.count)]
end
I need to generate a unique winner every time. How can I achieve this without making too many changes to this code?
b
is the maximum winner that I want.
b.times do
winner = participant[rand(participant.count)]
end
I need to generate a unique winner every time. How can I achieve this without making too many changes to this code?
There is already a method for that. Just use
Array#sample
:You can use
Array#delete_at
which deletes an item at specified index and returns the deleted item. We can now ensure that item once picked as winner will never be picked againYou can use
Array#sample
(as ndn suggested) if your program is okay to get all winners in one shot