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


Is this piece of PHP code efficent?





DandDer
I got this piece of code from a google search of weighted random...but i just cant help but shake the feeling that it is insanely innefficent. Can anyone take a quick look over it and tell me if it will add more load to the already overloaded server 2 Sad

Code:
$weights= array(
   "1" => 10000000, //Dustin
   "2" => 9800000, //Carl
   "3" => 11000000, //Pat
   "4" => 1 //Pat's Mom
   );

function weightRandom($weights){
    /*******************************\
    |   Copyright 2006 Erik Eloff   |
    | This code is free to use only |
    | if you leave this notice here |
    \*******************************/
     
    $loop;
    $topNum;
    $randNum;

    foreach( $weights as $val ){
        $topNum += $val;
    }
    $randNum = rand()%$topNum+1;
    $topNum = 0;
    foreach( $weights as $key => $val ){
        $topNum += $val;
        if( $val > 0 ){
            if( $topNum >= $randNum ){
                return $key;
            }
        }
    }
}
snowboardalliance
DandDer wrote:
I got this piece of code from a google search of weighted random...but i just cant help but shake the feeling that it is insanely innefficent. Can anyone take a quick look over it and tell me if it will add more load to the already overloaded server 2 Sad

Code:
$weights= array(
   "1" => 10000000, //Dustin
   "2" => 9800000, //Carl
   "3" => 11000000, //Pat
   "4" => 1 //Pat's Mom
   );

function weightRandom($weights){
    /*******************************\
    |   Copyright 2006 Erik Eloff   |
    | This code is free to use only |
    | if you leave this notice here |
    \*******************************/
     
    $loop;
    $topNum;
    $randNum;

    foreach( $weights as $val ){
        $topNum += $val;
    }
    $randNum = rand()%$topNum+1;
    $topNum = 0;
    foreach( $weights as $key => $val ){
        $topNum += $val;
        if( $val > 0 ){
            if( $topNum >= $randNum ){
                return $key;
            }
        }
    }
}


inefficient? I don't think so, not significantly though I haven't tried to do weighted random.

Will it overload a server? No way. Unless you use massive amounts of data and it is run constantly, it will have no effect.

So you might be able to optimize it, like by knowing the total of all values ahead of time (the first loop), but it is not too bad.
DandDer
weighted random is terribly hard to do next to regular random...i honestly had no idea how to do it for the longest time and then i just broke down and went onto google to see if someone else had solved the problem.

Its good to hear that it wont overload the server, and their definatly wont be massive ammounts of data running through it (the numbers i chose were like a worst case senerio times a thousand.

I also know nothing about the code so it would be impossible to optimize, i only recognize the odd thing that is the same from java...ohh how i love java Razz
Mgccl
very efficient because it is running on O(n) time and it's Ω(n) too... but try to test it's speed with mine Randomlib I wrote a long time ago....
mine one could be faster because I had some small optimizations.... so try me Smile
DandDer
i woulda thought it was at least O(nlogn)...but as my understanding of run time goes i dont think that big omega of n is good...since isnt big omega saying the function will run in n or more...ohh well its irreivent i guess, probably just more shocked that i was actually able to apply something i learned from algorigms Razz


Your algoritm is probably much more efficent, but i have no idea what it is saying nor how to use it so i would feel more comfortable working with mine (i'm usually more of a simplest method is alwase the best method kinda person). Nonetheless its nice to know that you guys are here thank you for your help Cool
Mgccl
mine is almost exactly the same as yours...
mine got some other powers... like unique weighted random...

check it out Smile
Related topics
Show PHP Code
Mail with PHP (Simple Tutorial)
Php Code Trouble Set-up
PHP CODE INCLUSION
mySQL Query/PHP code - get the highest value...
my PHP code benchmark script that got some problems
PHP code snippit?
CSS for making images go through a php code.
Gradient Fill Cell
What is a good or bad php code?
I want PHP Code to ......
Help With Simple PHP Code Snipit
removing DirectAdmin part of URL makes php code work
Highlight PHP-code in a HTML-document
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.