/*
Weighted Random Ver 1.0 by Mgccl(mgcclx@gmail.com)
Useage: input an numbered array
$array[$i][’s’] is the string you want to return
$array[$i][’w'] is the weight of the string
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, true);
Note: You need the math functions I wrote in
order to use GCD, you can find them here:
[url]http://www.webdevlogs.com/2006/11/21/some-math-functions/[/url]
*/
function rand_string_pro($seed, $gcd = false){
$count = count($seed);
if($gcd === true){
foreach($seed as $var){
$gcd_array[] = $var[‘w’];
}
$gcd = math_gcd_array($gcd_array);
if($gcd != 1){
$i = 0;
while($i < $count){
$seed[$i][‘w’] /= $gcd;
++$i;
}
}
unset($gcd, $gcd_array);
}
$i = 0;
while($i < $count){
$n = 0;
while($n < $seed[$i][‘w’]){
$key[] = $i;
++$n;
}
++$i;
}
return $seed[$key[mt_rand(0, count($key)-1)]][’s’];
}
Tell me what do you think, this should be the fastest weighted random script 🙂