[Tutor] Weighted Random numbers
boncelet at ee.adfa.edu.au
Mon Feb 14 00:44:23 CET 2000
(Bill wants weighted random numbers for a banner ad system)
Bill Anderson wrote:
> (x-posted to comp.lang.python in the hopes of reaching a wider audience
> [esp re: NumPY]... hope nobody minds)
> "Wesley J. Chun" wrote:
> > 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.
> 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.
These two methods are the same, differing only in an unimportant
normalization factor. To see this, imagine the weights were 1,2,2,5.
The second method would create a list of 10 items, [a,b,b,c,c,d,d,d,d,d].
The first, in effect, creates a list of 100 items (e.g., "a" is replicated 10
The real differences are in implementation. The first can be implemented
with real numbers and is usually done with a sequence of "if" statements.
The second is simpler if the weights are simple integers.
In your application the speed differences are probably irrelevant.
Charles Boncelet, University of Delaware,
On sabbatical at ADFA, Canberra Australia,
Home Page: http://www.ece.udel.edu/~boncelet/
More information about the Python-list