Our Scripts Vault contains many game scripts that you can use to create your own game!
$array[] = 'string';
$array[] = 'weight 1';$array[] = 'weight 2, have higher chance to be chosen than weight 1';$array[] = $array[1];echo $array[array_rand($array)];
$array[] = 'test for speed of no weight arrays';$array[] = 'how fast is this';$array[] = 'the final result for this * 2';
$array[] = 'test for weight';$weight[] = 30000;$array[] = 'with HUGE numbers';$weight[] = 2000;$array[] = 'final result for this * 5';$weight[] = 50000;
$array[]= 'test for weight array only on some';$weight[] = 20000;$array[]= 'final result for this * 3';
$items[] = array("weight" => 1, "item" => "sword"); $items[] = array("weight" => 5, "item" => "dagger"); echo get_random_item($items, "weight", "item"); function get_random_item($content, $weight, $value) { // Add all the weights together. $total_weight = 0; foreach ($content as $entry) { $total_weight += $entry[$weight]; } // Generate random selection between 0 and totalweight - 1 $pick = mt_rand(0, $total_weight - 1); // Find the content entry that matches the random number. $cumulative_weight = 0; foreach ($content as $entry) { $cumulative_weight += $entry[$weight]; if ($pick < $cumulative_weight) { return $entry[$value]; } } return false; }
<?phpfunction item($item, $weight = array()) //$weight variable is optional{ $pad = array_pad(array(), count($item), 1); //Change 1 to whatever you want the default weight to be $weight = $weight+$pad; ksort($weight); //Re-order the array $weight2 = $weight; //Find smallest number of all weights asort($weight2); foreach($weight as $key=>$value) { $weight[$key] = round($value / $weight2[0]); //Divide all numbers to get smallest ratio with whole numbers } $total = 0; foreach($weight as $w) { $total += $w; //Find total weight } $values_norm = array(); foreach($weight as $v) { $values_norm[] = $v / $total; //Ratios... } $r = rand(0, 32767) / 32767; //Random number between 0 and 1 $n = 0; //Check ratios and compare with the same random number while ($r > $values_norm[$n]) { //Loop through each item ++$n; } $result = $item[$n]; return $result; }?>
/* Weighted Random Ver 1.2 by Mgccl(mgcclx@gmail.com) http://www.webdevlogs.com Update: Nov/29/06 Faster Speed 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); */function rand_string_pro($seed, $weighted = false){ $count = count($seed); if($weighted === false){ return $seed[mt_rand(0, $count - 1)]; }else{ $i = 0; $n = 0; $num = mt_rand(0, array_sum($weighted) + ($count-count($weighted))); while($i < $count){ if(empty($weighted[$i])){ ++$n; }else{ $n += $weighted[$i]; } if($n >= $num){ break; } ++$i; } return $seed[$i]; }}
function item($item, $weight = array()) //$weight variable is optional{ $pad = array_pad(array(), count($item), 1); //Change 1 to whatever you want the default weight to be $weight = $weight+$pad; ksort($weight); //Re-order the array $weight2 = $weight; //Find smallest number of all weights asort($weight2); foreach($weight as $key=>$value) { $weight[$key] = round($value / $weight2[0]); //Divide all numbers to get smallest ratio with whole numbers } $total = 0; foreach($weight as $w) { $total += $w; //Find total weight } $values_norm = array(); foreach($weight as $v) { $values_norm[] = $v / $total; //Ratios... } $r = rand(0, 32767) / 32767; //Random number between 0 and 1 $n = 0; //Check ratios and compare with the same random number $count = count($values_norm)-1; while (($n <$count) && ($r > $values_norm[$n]) ) { ++$n; } $result = $item[$n]; return $result;}
<?phpfunction weighted_random_element($array, $weights = false){ $elements = count($array); if (!$weights) { return $array[(int) mt_rand(0, $elements - 1)]; } if ($elements != count($array)) { # You're on your own here, I don't understand how it's supposed to treat input with only some weights specified throw new Exception(); } # Get weights to lowest-to-highest order while maintaining association with $array array_multisort($weights, $array); # Add up all weights $total = array_sum($weights); $placement = mt_rand(0, $total); # This might have to be from 1 to total, I can't figure which introduces a bias # Everything from 0-lowest weight goes to that element, lowest weight to second lowest goes to the second lowest weighted element, etc for ($i = $sum = 0; $i < $elements; $sum += $weights[++$i]) { if ($placement <= $sum) { return $array[$i]; } } return $array[$elements - 1];}$test_ar = array('a', 'b', 'c', 'd');$weights = array(4, 3, 20, 1);$results = array();for ($i = 0; $i < 1000000; ++$i){ ++$results[weighted_random_element($test_ar, $weights)];}ksort($results);print_r($results);?>
Personally not my approach, instead I tend to go with bell curve algorithm I made.. Given two inputs the variance between the two dictate the outcome. Meaning if they are close to each other they are in the middle of the curve, the farther away they are from each other the more then hit the downslope of the curve.