Thread: Generate a "random" number with some weight!

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2000
Posts
73
Rep Power
19
Anyone knows/suggests the logic to get a random number with some specific weights?
A clear example: banner serving, from a banner pool, you get a banner, and that banner has a weight.
One solution I've seen: create an array with the heavier elements have more copies in the array.

So that: a banner with weight 10 and there are 10 other banners with weight 1, the pool banner will have 11 elements, and the banner with weight 10 appears 10 times!

What I want to ask is another solution(or algorithm)? Or is this one the best algorithm?

------------------
http://new.123finder.com/ - Helps you find cool domains for FREE & Registers it for only \$14.95/yr
http://www.guideclick.com/ - Qualified webmaster resources (affiliate programs, web design, tips)

------
Son
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2000
Posts
45
Rep Power
19
Yes, that probably is the best way - I can't think of any other way to generate a weighted random number. A lot depends on a case-to-case basis, but this is probably the best general way.

3. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2000
Location
Portland, Or USA
Posts
3
Rep Power
0
you could loop on each banner by the number of the weight you want to give it. For example if I want to give:
Banner A = 10
Banner B = 7
Banner C = 1
Loop on banner A 10 times and keep the largest number by comparing the first and second number...keep the largest...compare against the 3rd...keep the largest and so on.

loop on B 7 times and keep the largest number

loop on C only one time.

The A, B or C with the largest number gets to be the banner posted.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2000
Posts
73
Rep Power
19
Is that efficient? I mean the speed? You used quite a lot of loops, and that thing would effectively decrease its speed, and put more work on the web server

------------------
http://new.123finder.com/ - Helps you find cool domains for FREE & Registers it for only \$14.95/yr
http://www.guideclick.com/ - Qualified webmaster resources (affiliate programs, web design, tips)

------
Son