Alternately,
<?php
$values = array(
// Number => Weighted chance
1 => 1,
2 => 1,
3 => 5, // 5x more likely than 1,2
4 => 3 // 3x more likely than 1,2
);
// The total of all the chances
$total = array_sum($values);
// Will track the sum of all tested chances
$i = 0;
// The number we're looking to find
$find = mt_rand(1,$total);
// Loop through value=>chance array
foreach( $values as $val => $chance ) {
// Add current chance to the running total
$i += $chance;
// Check if the running total equals or exceeds the number we're trying to find
if( $find <= $i )
// If so, break out of the loop
break;
}
// The last assigned value before breaking will contain the number
echo $val;
?>
The process is more complex, but the input is more simplified.