homepage Welcome to WebmasterWorld Guest from 23.23.12.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
weight based selection
randomly select following trend of url
phparion

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3688417 posted 12:17 pm on Jul 2, 2008 (gmt 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

 

vincevincevince

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



 
Msg#: 3688417 posted 12:29 pm on Jul 2, 2008 (gmt 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

phparion

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3688417 posted 12:49 pm on Jul 2, 2008 (gmt 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?]

vincevincevince

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



 
Msg#: 3688417 posted 1:18 pm on Jul 2, 2008 (gmt 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;}
}

Scally_Ally

5+ Year Member



 
Msg#: 3688417 posted 1:26 pm on Jul 2, 2008 (gmt 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]

vincevincevince

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



 
Msg#: 3688417 posted 1:31 pm on Jul 2, 2008 (gmt 0)

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

phparion

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3688417 posted 4:53 am on Jul 3, 2008 (gmt 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

vincevincevince

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



 
Msg#: 3688417 posted 5:16 am on Jul 3, 2008 (gmt 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.

s_mk

5+ Year Member



 
Msg#: 3688417 posted 1:16 pm on Sep 24, 2008 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved