weighted_randomizer
weighted_randomizer copied to clipboard
I get repeated items
@inserciones = Adblock.where(code: @sponsorsection).inject({}) {|h, p| h[p.id] = p.ranking; h}
@ofertas_select2 = WeightedRandomizer.new(@inserciones)
@ofertas_select = @ofertas_select2.sample(3)
@coupons = []
@ofertas_select.each do |o|
@insert = Adblock.find(o)
@oferta = @insert.coupon
if @oferta.publicada == true
@coupons << @oferta
end
end
Is returning random results, and sometimes the same item is picked 3 times, there are 3 items to chose { one => 3, two => 2, three => 1}
Is there a way to avoid repeating ?
help please
anyone?
@pmaojo: I agree, there should at least be a #sample_without_replacement
. As mentioned here, this is disanalogous to Ruby's own Array#sample
.
@jaysonvirissimo I ended using other gem: Pickup
@inserciones = Adblock.where(code: @sponsorsection).inject({}) {|h, p| h[p.id] = 1 + p.ranking; h}
@ofertas_select2 = Pickup.new(@inserciones, uniq: true)
n = @inserciones.count
@ofertas_select = @ofertas_select2.pick(n)
@ofertas_select.each do |o|
@insert = Adblock.find(o)
@oferta = @insert.coupon
if @oferta.publicada == true
@coupons << @oferta
end
end