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


PHP code showoff - Weighted Random





Mgccl
Code:
/*
        Weighted Random Ver 1.1 by Mgccl(mgcclx@gmail.com)
        Update: Nov/25/06 allow non-weighted random, seprate the
        string storing array and the weight storing array
        Useage: input $array[$i] = 'string' format(where $i is a number)
        $array[$i]is the string you want to return
        $weight[$i]is the weight of the string
        if one of the $array[$i] does not have a
        $weight[$i] as a match, it automatically
        set $weight[$i] as 1
        To allow use weighted function, call the function like this
        rand_string_pro($array, $weight);
        you can allow the function to find the GCD
        sometimes it speeds up the script
        To use GCD, use the function like
        rand_string_pro($array, $weight, true);
       
        Note: You need the math functions I wrote in
        order to use GCD, you can find them here:
        http://www.webdevlogs.com/2006/11/21/some-math-functions/
        */

function rand_string_pro($seed, $weighted = false, $gcd = false){
        $count = count($seed);
   if($weighted === false){
      return $seed[mt_rand(0, $count - 1)];
   }else{
       if($gcd === true){
            if(count($weighted) != $count){
            }else{
               foreach($weighted as $var){
                  $gcd_array[] = $var;
               }
               $gcd = math_gcd_array($gcd_array);
               if($gcd != 1){
                  $i = 0;
                   while($i < count($gcd_array)){
                       $weighted[$i] /= $gcd;
                      ++$i;
                   }
               }
                   unset($gcd, $gcd_array);
           }
       }
           $i = 0;
           while($i < $count){
              if(empty($weighted[$i])){
                 $key[] = $i;
              }else{
                   $n = 0;
                   while($n < $weighted[$i]){
                           $key[] = $i;
                           ++$n;
                   }
               }
                   ++$i;
           }
           return $seed[$key[mt_rand(0, count($key)-1)]];
      }
}


Tell me what do you think, it should be the fastest code for weighted Random out there Smile
Diablosblizz
Quote:
Note: You need the math functions I wrote in
order to use GCD, you can find them here:
http://www.webdevlogs.com/2006/11/21/some-math-functions/


Needing a something else for scripts is kinda dumb. Somehow you should include it into the code (I never checked out the site so I don't know what you've used).
Mgccl
a function that calculates the Greatest Common Denominator for an array of numbers...
Anyway... it is not completely necessary because with out it, it can still run smoothly...
except sometimes when you really need the GCD function, you can just download from that site...
snowboardalliance
Diablosblizz wrote:
Quote:
Note: You need the math functions I wrote in
order to use GCD, you can find them here:
http://www.webdevlogs.com/2006/11/21/some-math-functions/


Needing a something else for scripts is kinda dumb. Somehow you should include it into the code (I never checked out the site so I don't know what you've used).


Add...

Quote:
Code:

//greatest common denominator of 2 numbers
// thx to xJeff at lifelesspeople.com
function gcd($a, $b) {
  if ($b == 0) return $a;
  else return gcd($b, $a % $b);
}
//greatest common denominator of an array
//thx to pterodactyl at lifelesspeople.com
//for the use of array_splice(smart)
function math_gcd_array($array){
   while (count($array) > 1) {
       array_splice($array, 0, 2, gcd($array[0], $array[1]));
   }
   return $array[0];
}


...I think for the GCD part and the script will work fine.

Interesting script, could be useful. You should submit it somewhere like http://weberdev.com/
Mgccl
just updated the script.... a bit better Smile
Related topics
Show PHP Code
PHP CODE INCLUSION
mySQL Query/PHP code - get the highest value...
my PHP code benchmark script that got some problems
PHP code snippit?
Contest for the fastest weighted Random script, all my $f
My final version of weighted random alpha
CSS for making images go through a php code.
Is this piece of PHP code efficent?
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
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.