For p in products-to-replace: c = int rand(@total - @chosen) For i in the sorted set of already-chosen items: last if c < i c++ Insert c into sorted set new{p} = total[c]