public function test_prive(){
$prize_arr = array(
array('id'=>'1','name'=>'獎品1','lv'=>'10'),
array('id'=>'2','name'=>'獎品2','lv'=>'20'),
array('id'=>'3','name'=>'獎品3','lv'=>'30'),
array('id'=>'4','name'=>'獎品4','lv'=>'10'),
array('id'=>'5','name'=>'獎品5','lv'=>'10'),
array('id'=>'6','name'=>'獎品6','lv'=>'20'),
);
$pr_arr = array_column($prize_arr,'lv');
$count_num = array_sum($pr_arr);
$rkay = mt_rand(1,$count_num);
echo "<pre>";
print_r($pr_arr);
echo $count_num."<br>";
echo $rkay."<br>";
$num = 0;
foreach($pr_arr as $k=>$v){
if($rkay > $num+$v){
$num += $v;
}else{
$key = $k;
break;
}
}
echo $key;
}
function get_rand($proArr) {
$result = '';
//概率陣列的總概率精度
$proSum = array_sum($proArr);
//概率陣列迴圈
foreach ($proArr as $key => $proCur) {
$randNum = mt_rand(1, $proSum);
if ($randNum <= $proCur) {
$result = $key;
break;
} else {
$proSum -= $proCur;
}
}
unset ($proArr);
return $result;
}