Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > University Math Help > Advanced Probability and Statistics
Reply
 
Thread Tools Display Modes
  #1  
Old November 22nd, 2008, 12:40 PM
m3p m3p is offline
Newbie
 
Join Date: Nov 2008
Location: Cumberland, Rhode Island USA
Posts: 2
Country:
Thanks: 0
Thanked 0 Times in 0 Posts
m3p is on a distinguished road
Default looking for algorithm for weighted random choice

The subject line probably doesn't quite call it what it's supposed to be called but, anyway, here's the problem:

Just for the sake of argument, say there are 5 possibilities (each "possibility" represents someone's blog feed for instance.) Over the course of time, each feed receives votes on how interesting it is to the audience reading the feeds.

So, feed1 has received 41 votes, feed2 - 25, feed3 - 14, feed4 - 10, feed5 -8.

What would be the formula for randomly choosing one of the five feeds, while at the same time giving the feeds with a higher number of votes, a better chance at getting selected? In other words, how to randomly choose one of the feeds while factoring in their popularity (or unpopularity, as the case may be.)

Thanks in advance for any replies.
Reply With Quote
Advertisement
 
  #2  
Old November 22nd, 2008, 03:28 PM
Super Member
 
Join Date: Mar 2008
Posts: 327
Country:
Thanks: 11
Thanked 154 Times in 128 Posts
awkward is a jewel in the roughawkward is a jewel in the roughawkward is a jewel in the roughawkward is a jewel in the rough
Default

Quote:
Originally Posted by m3p View Post
The subject line probably doesn't quite call it what it's supposed to be called but, anyway, here's the problem:

Just for the sake of argument, say there are 5 possibilities (each "possibility" represents someone's blog feed for instance.) Over the course of time, each feed receives votes on how interesting it is to the audience reading the feeds.

So, feed1 has received 41 votes, feed2 - 25, feed3 - 14, feed4 - 10, feed5 -8.

What would be the formula for randomly choosing one of the five feeds, while at the same time giving the feeds with a higher number of votes, a better chance at getting selected? In other words, how to randomly choose one of the feeds while factoring in their popularity (or unpopularity, as the case may be.)

Thanks in advance for any replies.
Hi m3p,

If I may generalize your question slightly, I think you are asking for an algorithm for making a random choice from 1 of n objects in such a way that the probability of choosing object i is p_i, where 0 \leq p_i and p_1 + p_2 + ... + p_n = 1.

On a computer, one way to do this is to generate a pseudo-random number u from a Uniform(0,1) distribution. Then choose object m where m is the least integer such that
\sum_{i=1}^m p_i > u.

For your example, you would take p_i to be the number of votes for feed i, divided by the total number of votes.
Reply With Quote
  #3  
Old November 23rd, 2008, 10:03 AM
m3p m3p is offline
Newbie
 
Join Date: Nov 2008
Location: Cumberland, Rhode Island USA
Posts: 2
Country:
Thanks: 0
Thanked 0 Times in 0 Posts
m3p is on a distinguished road
Default re: looking for algorithm for weighted random choice - solution

O.k., turns out the most concise phrase to use on search engines when looking for an answer to this question, is to
look for "weighted random numbers", or "weighted random number generator".

In any any case, I think I've found my answer and I thought I'd share it here in return for the possibility (pun
intended), there was for me to receive an answer from the knowledgeable members of this forum.

There were several pages I found that provided excellent approaches to the subject, but without question the most
elegant and efficient solution was found here. The Perl code (adapted from the PHP solution I found), is shown below:

001 @feeds = ('f1', 'f2', 'f3', 'f4', 'f5' );
002 @votes = (41, 25, 14, 10, 8 );
003
004 $tot= eval join '+', @votes; # total in this case = 98
005
006 $rand = rand(1000);
007
008 $offset = 0;
009 $indx=0;
010 foreach $vote (@votes) {
011 $offset += ($vote / $tot) * 1000;
012 if ($rand <= $offset) {
013 return $indx;
014 }
015 $indx++;
016 }

What's going on:

lines 001-002: establish arrays of feed names and corresponding votes received
line 004: get the total number of votes
line 006: get a random number between 0-999
line 010: start looping thru the array of votes
line 011: for the current feed, find the ratio of votes to total votes, multiply that value times 1000,
add the result to $offset
line 012: if the random number obtained before entering the loop is less than or equal to $offset (i.e. the
high boundary for the particular feed's possibility range), then return a pointer to the "random" feed

In other words, feed f1 has roughly a 41/98 (41%) chance of being selected, feed f2 - 25%, etc.
So out of 1000 random numbers (0-999), if a number between 1-418 comes up, then feed f1 "wins", between 419-673,
feed f2 wins, etc.

The beauty of this solution is that it doesn't use up a lot of memory, like some solutions I found, it doesn't
care how many possibilities there are, what their weights are, or what the total of the weights is, and most importantly in my case, it's so easy to understand.

So... thanks to all for tolerating my question.
---------------------------------------------------
SponsorWorks.net
Free Online Marketing and Advertising Etools.
---------------------------------------------------
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 04:48 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.