function get_gift(){
//拼裝獎(jiǎng)項(xiàng)數(shù)組
// 獎(jiǎng)項(xiàng)id,獎(jiǎng)品,概率
$prize_arr = array(
'0' => array('id'=>1,'prize'=>'平板電腦','v'=>0),
'1' => array('id'=>2,'prize'=>'數(shù)碼相機(jī)','v'=>0),
'2' => array('id'=>3,'prize'=>'音箱設(shè)備','v'=>0),
'3' => array('id'=>4,'prize'=>'4G優(yōu)盤','v'=>5),
'4' => array('id'=>5,'prize'=>'10Q幣','v'=>0),
'5' => array('id'=>6,'prize'=>'空獎(jiǎng)','v'=>5),
);
foreach ($prize_arr as $key => $val) {
$arr[$val['id']] = $val['v'];//概率數(shù)組
}
$rid = $this->get_rand($arr); //根據(jù)概率獲取獎(jiǎng)項(xiàng)id
$res['yes'] = $prize_arr[$rid-1]['prize']; //中獎(jiǎng)項(xiàng)
unset($prize_arr[$rid-1]); //將中獎(jiǎng)項(xiàng)從數(shù)組中剔除,剩下未中獎(jiǎng)項(xiàng)
shuffle($prize_arr); //打亂數(shù)組順序
for($i=0;$icount($prize_arr);$i++){
$pr[] = $prize_arr[$i]['prize']; //未中獎(jiǎng)項(xiàng)數(shù)組
}
$res['no'] = $pr;
// var_dump($res);
if($res['yes']!='空獎(jiǎng)'){
$result['status']=1;
$result['name']=$res['yes'];
}else{
$result['status']=-1;
$result['msg']=$res['yes'];
}
//return $result;
var_dump($result);
}
//計(jì)算中獎(jiǎng)概率
function get_rand($proArr) {
$result = '';
//概率數(shù)組的總概率精度
$proSum = array_sum($proArr);
// var_dump($proSum);
//概率數(shù)組循環(huán)
foreach ($proArr as $key => $proCur) {
$randNum = mt_rand(1, $proSum); //返回隨機(jī)整數(shù)
if ($randNum = $proCur) {
$result = $key;
break;
} else {
$proSum -= $proCur;
}
}
unset ($proArr);
return $result;
}