FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


A weight problem





Mgccl
Code:

$thearray[] = array("url"=>"http://localhost",
                                "weight"=>"8");
$thearray[] = array("url"=>"http://www.php.net",
                                "weight"=>"1");

and now I want randomly chose an array of url, but each url have different weight (defined by the weight array), and have the chance of chose the array which contain http://localhost 8 times larger than the other.
is there any way to do that? and I want a good solution because $thearray[] can be hundreds
shamil
Mgccl wrote:
Code:

$thearray[] = array("url"=>"http://localhost",
                                "weight"=>"8");
$thearray[] = array("url"=>"http://www.php.net",
                                "weight"=>"1");

and now I want randomly chose an array of url, but each url have different weight (defined by the weight array), and have the chance of chose the array which contain http://localhost 8 times larger than the other.
is there any way to do that? and I want a good solution because $thearray[] can be hundreds

Code:
<?
$thearray[] = array("url"=>"http://localhost",
                                "weight"=>"8");
$thearray[] = array("url"=>"http://www.php.net",
                                "weight"=>"1");
                        $therand= array();
for($i = 0; $i < count($thearray); $i++) for($j = 0; $j < $thearray[$i]["weight"]; $j++) $therand[] = $i;
echo "Random URL => ".$thearray[$therand[rand(0, count($therand)-1)]]["url"];
?>

It is impossible to take random url according to weight unless you take all urls weight into account. So it seems this is the optimum way.
Mgccl
this is good... but if someone made the weight too big will it make it goes sooo slow?
is there any other way or I just got to make restriction to how much weight people can put
shamil
Mgccl wrote:
this is good... but if someone made the weight too big will it make it goes sooo slow?
is there any other way or I just got to make restriction to how much weight people can put

I think it is not good to trust people make weights on their own because they will put greater value. Even if you restrict max weight number all they will put that max number too and making your weight idea useless.
As an answer to your question: It takes too small time that it doesn't make a big difference in time for script execution. But when i tested for big number script failed. The reason was obviously at the number i have put to array index(80000); I think there is a limit in max length of arrays in php but not sure. If it is true I am sorry to inform you that that script would not work for big numbers. I recommend you not to use weight and make all urls have same right to be selected if possible.
Related topics
CSS problem...
Game Designer
Nutrisystem:Weight Watchers:Adkins: do programs work for U?
Css problem
Tyra Banks gone nuts?
My Religious Rant
fujitsu or lenovo, which will you suggest
Skinny Bitch diet.
How to loose weight...?
You fat or skinny? dont be...
Have a weight problem?
Running might not be the ideal form of exercise, especially
God exists - and here's the proof
The Fat Lady
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.