# [Tutor] Weighted Random numbers

Bill Anderson bill.anderson@libc.org
Sat, 12 Feb 2000 15:40:43 -0700

```(x-posted to comp.lang.python in the hopes of reaching a wider audience
[esp re: NumPY]... hope nobody minds)

"Wesley J. Chun" wrote:
>
>     > Date: Fri, 11 Feb 2000 18:19:54 -0700
>     > From: Bill Anderson <bill.anderson@libc.org>
>     >
>     > I am working on a bannerad system in Python (for Zope). I have most of
>     > it functional, but I need to be able to weight certain selections, just
>     > as with any of the myriad perl implementations. Anyone have any pointers
>     > on a way to do this in python?
>
> the best way to do this is to choose from a weighted
> list of 1 to 100 say, with each number representing a
> percentage or proportion of the time an ad should be
> chosen.  each ad would get a "range" based on the %age
> of the time it should show.  the "odds" of all the
> individual ads summed together should be 100.
>
> may get 50% and be given the segment from 1-50.  another
> ad, not as prominent may get 25%, so it gets 51-75.  a
> 3rd and 4th ad get 10% each, so 76-85, and 86-95, re-
> spectively, and the 5th and final ad gets 5%, 96-100.

That's a lot like what I was thinking. I as considering building a list
of banners, with the weight being the number of times the ad was in the
list. Then using whrandom, I'd select one.

The only issue I see so far with the percentage method, is that it makes
management more of a headache. In order to set weights, I would need a
master page with all the banners, their weight percentage, and the
total.

Hmmm ... some random unweighted-thoughts ...
I think the difference in these two methods are in the type of
weighting.
In my method, the weight is against a non-weighted ad, that is, an ad
with weight 1. An ad with weight 5 is 5x more likely to be seen than a
ad with weight 5 will be seen 5% of the time. The difference is sublt,
but important, IMHO.

I guess unless I (or anyone else ;) can come up with a different method,
I'll probably wind up implementing them both, with an option to select
at system creation time.

Bill
--
In flying I have learned that carelessness and overconfidence are
usually far more dangerous than deliberately accepted risks.
-- Wilbur Wright in a letter to his father, September 1900

```