Welcome to WebmasterWorld Guest from 54.145.44.134

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

weight based selection

randomly select following trend of url

     

phparion

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

WebmasterWorld Senior Member 5+ Year Member



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

vincevincevince

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

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



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

phparion

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

WebmasterWorld Senior Member 5+ Year Member



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?]

vincevincevince

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

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



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;}
}

Scally_Ally

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

5+ Year Member



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]

vincevincevince

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

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



Looks about right to me :) Good luck with it.

phparion

4:53 am on Jul 3, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member




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

vincevincevince

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

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



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.

s_mk

1:16 pm on Sep 24, 2008 (gmt 0)

5+ Year Member



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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month