On Mon, Nov 26, 2012 at 3:23 PM, Damjan Rems <lists / ruby-forum.com> wrote:
> Since I am not computer graduate (electro engineer as mater of fact) I
> haven't studied lots of computer algorithms. And since english is not my
> first language I also find it difficult to search google.
>
> What I would like to accomplish is create an algorithm for showing ads
> based on priority (or number of times to display). Priority would define
> no. of times ad would be displayed.
>
> Example: We have 3 ads to display. If our web page is hit 6 times ads
> should be displayed:
> add1 1 time
> add2 5 times
> add3 10 times
> ... and theoretically
> addn 10 times
>
>
> Please direct me to a solution.

I'm not sure I understand correctly, since 1 + 5 + 10 + ... +
theoretically 10 is not 6, so I don't really follow your example. If
you want to choose an ad among a set of ads each with a weigth, so
overall the number of times each ad is shown is proportional to the
weight:

r = rand(100) # 100 should be the sum of the weights, so 1 + 5 + 10 + ... + 10
case r
  when 0
     "add1"
  when 1..5
     "add2"
  when 6..15
     "add3"
[...]
end

Hope this helps,

Jesus.