public function choujiang()
{
// 奖品数组
$proArr = [
['id' => 1, 'name' => '特等奖', 'v' => 1],
['id' => 2, 'name' => '一等奖', 'v' => 4],
['id' => 3, 'name' => '二等奖', 'v' => 5],
['id' => 4, 'name' => '三等奖', 'v' => 10],
['id' => 5, 'name' => '四等奖', 'v' => 30],
['id' => 6, 'name' => '谢谢惠顾', 'v' => 50],
];
dump($this->get_rand($proArr));
}
private function get_rand($proArr)
{
$result = array();
foreach ($proArr as $key => $val) {
$arr[$key] = $val['v'];
}
dump("抽奖概率");
dump($arr);
// 概率数组的总概率
$proSum = array_sum($arr);
asort($arr);
// 概率数组循环
foreach ($arr as $k => $v) {
$randNum = mt_rand(1, $proSum);
if ($randNum <= $v) {
$result = $proArr[$k];
break;
} else {
$proSum -= $v;
}
}
return $result;
}