weighted_randomizer icon indicating copy to clipboard operation
weighted_randomizer copied to clipboard

I get repeated items

Open pmaojo opened this issue 7 years ago • 4 comments

    @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 ?

pmaojo avatar Jan 31 '18 00:01 pmaojo

help please

pmaojo avatar Feb 21 '18 20:02 pmaojo

anyone?

pmaojo avatar Mar 01 '18 22:03 pmaojo

@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 avatar Oct 22 '18 19:10 jaysonvirissimo

@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

pmaojo avatar Oct 24 '18 11:10 pmaojo