Welcome to WebmasterWorld Guest from 54.196.231.129

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

weight based selection

randomly select following trend of url

     
12:17 pm on Jul 2, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 9, 2005
posts:817
votes: 0


Hi

i have a table where i store URLs with their ration to appear e.g

example1.com (50%)
example2.com (30%)
example3.com (50%)
example4.com (40%)

Now I want to make a code which can select randomly the URLs based on the intensity/ratio/trend value of it. for example in above sample data the example1.com has biggest trend to be selected randomly, example2.com has the lowest trend value so it should appear the least in random selections. it is done on many PPC systems.

thank you

12:29 pm on July 2, 2008 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


Add up all the 'percentages' (SUM()) function in MySQL. Now, take that maximum number (170% in your example) and pick a random number in that range:
$pick=rand(0,170);

Finally, add up the row weights cumulatively until you go past $pick... (e.g. $pic = 70)

start .......... = 0
example1.com +50%=50
example2.com +30%=80 << stop here, 80>70 = example2.com
example3.com +50%=130
example4.com +40%=170

12:49 pm on July 2, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 9, 2005
posts:817
votes: 0


hmm, I am working on it and will let you know.

[should i use ASC or DESC while selecting frequencies of urls from table before using cumulative formula?]

1:18 pm on July 2, 2008 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


It doesn't matter actually! You instinctively think it does... but it doesn't.

$dh="SELECT `site`,`percentage` FROM `table`";
while ($r=mysql_fetch_row($dh))
{
$cumulative+=$r[1];
if ($cumulative>$pick) {print "Use $r[0]";break;}
}

1:26 pm on July 2, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Aug 3, 2005
posts:269
votes: 0


maybe something like:

$runTot = 0;
$ret = array();
$i=0;
while($row = mysql_fetch_assoc($result)){

$ret[$i]["min"] = $runTot;
$ret[$i]["site"] = $row["site"];

$runTot += $row["ration"];
$i++;

}

$pick = random(0, $runTot);
for($i=count($ret); $i>0; $i--){
if($ret[$i]["min"] < $pick){
echo $ret[$i]["site"];
exit;
}
}

This is really crude i know and i havent tested it, but you see where i am going with it.

Ally

[edited by: Scally_Ally at 1:27 pm (utc) on July 2, 2008]

1:31 pm on July 2, 2008 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


Looks about right to me :) Good luck with it.
4:53 am on July 3, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 9, 2005
posts:817
votes: 0



thank you very much,

Just a second thought if we can achieve this php functionality with this mysql query?

SELECT site FROM table ORDER BY ratio * RAND() DESC

5:16 am on July 3, 2008 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
posts:4847
votes: 0


Unfortunately, that won't do the cumulative bit. You could preset all the cumulative values into another column and do it with a single select.
1:16 pm on Sept 24, 2008 (gmt 0)

New User

10+ Year Member

joined:Sept 13, 2005
posts: 25
votes: 0


ok, tried to throw a link to a great article about just this, but I guess it's not an authoritative source (...?), so I'll just post the piece of relevent code here.

This algorithm is a bit more efficient then most ways to do random selections from an array with weight because it doesn't rely on creating an auxillary array with a position of each of the weights. Its elegant and simple.

You only have to turn the weights into a percentage of the total weight, and then pass the array into the following function:


*function w_rand($weights) {
* $r = mt_rand(1,1000);
* $offset = 0;
* foreach ($weights as $k => $w) {
** $offset += $w*1000;
** if ($r <= $offset) return $k;
* }
*}

Hope this helps someone.

_greg

PM me for a link to the article if you want.