Rate this script:  I Love it  /   I Hate it

weighted distribution functions


Code


<?
# users "user"=>weight, "user"=>weight ...
$users = array("manager"=>1,"peon"=>4, "middlemngr"=>3, "sales"=>3, "other peon"=>4);
asort($users);

############################################################################################################
function distrib($arr){
    $weight_total = array_sum($arr);
    $vals = array_values($arr);
   
    // loop through and figure out the percentage for each user.
    while ( list($key, $val) = each($arr) ){
       $curr = $val;
       //$percent =  (($curr * 100) / $total); // percentage of highest..
       $percent(($curr / $weight_total) * 100); // this is for % of sum(total)
       $percent_int = floor($percent);
           $percent_float = number_format($percent, 1);   
       
       // now set up our return array.. "user"=>percentage, "user"=>percentage ...
       $ret[$key] = $percent_float;
    }//wend
  // send it back
  return $ret;
}// end function

############################################################################################################
// this function spits back a placement for a user..
// theoretically this should have an even distribution based on our distribution list.
// dist_arr = output from distrib()
function determine_placement($dist_arr, $rndval=""){
    if ($rndval == "") { $rndval = mt_rand(0,100); } //set a rnd val
    $running = 0; // private $running default
    while (list($key, $val) = each ($dist_arr)){
        $running  = $val;
        if ($rndval < $running) {
            return $key;
        }
    }//wend
    return false;
}//end function

############################################################################################################
// just to test the output
function test_output($dist_arr){
    while (list($key, $val) = each ($dist_arr)){
        echo "<td width=\"$val%\">$key  ($val%)</td>\n";
    }//wend
}//end function

$gen_distrib = distrib($users);
?>

<table width=1 border=1 cellpadding=0 cellspacing=0>
<tr>
<? test_output(distrib($placement)); ?>
</tr>
</table>

 

 

 
weighted distribution functions scripts | weighted distribution functions snippet | weighted distribution functions example | weighted distribution functions tutorial | weighted distribution functions code