Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I get repeated items #2

Open
pmaojo opened this issue Jan 31, 2018 · 4 comments
Open

I get repeated items #2

pmaojo opened this issue Jan 31, 2018 · 4 comments

Comments

@pmaojo
Copy link

pmaojo commented Jan 31, 2018

    @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
Copy link
Author

pmaojo commented Feb 21, 2018

help please

@pmaojo
Copy link
Author

pmaojo commented Mar 1, 2018

anyone?

@jaysonvirissimo
Copy link

@pmaojo: I agree, there should at least be a #sample_without_replacement. As mentioned here, this is disanalogous to Ruby's own Array#sample.

@pmaojo
Copy link
Author

pmaojo commented Oct 24, 2018

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants