I want to display a couple records randomly. First thing I tried was this:
thing1 = Thing.offset(rand(Thing.count)).first
thing2 = Thing.offset(rand(Thing.count)).first
But that sometimes returns duplicates obviously. I guess not much of a problem with a lot of records, but now I'm curious. So I tried this:
random1 = rand(count)
random2 = rand(count)
until random1!=random2 do
random2 = rand(count)
end
thing1 = Thing.offset(random1).first
thing2 = Thing..offset(random2)).first
But that's not perfect either, if you get, say, 4 and 5 as your random numbers, and record 4 was deleted, you still end up with a duplicate, because offset. Plus, and this is nitpicking, records directly after n deleted records are (n+1) times more likely to come up in this "random" selection.
What's the best way to get multiple unique random records?