//可控概率的随机数 function get_rand($data) { $result = ''; //概率数组的总概率精度 $sum = array_sum($data); //求和 25 //概率数组循环 foreach ($data as $key => $val) { $num = mt_rand(1, $sum); if ($num <= $val) { $result = $key; break; } else { $sum -= $val;//***** } } return $result; } //array = id => 概率 get_rand(array(1 => 5, 2 => 10, 3 => 15, 4 => 20)); //======================================================== function get_rand($arr) { $arr_sum = array_sum($arr); $rand_num = mt_rand(1, $arr_sum); $res = $last_num = 0; foreach($arr as $key => $val){ if($rand_num > $last_num && $rand_num <= $val + $last_num){ $res = $key; break; }else{ $last_num += $val; } } return $res; } for($i=0;$i<1000;$i++){ echo get_rand(array(1 => 200, 2 => 300, 3 => 100, 4 => 400)); }