您好,登錄后才能下訂單哦!
<?php
//計算中獎概率
function getRand($proArr){
$rs = '';//中獎結(jié)果
$proSum = array_sum($proArr);//概率數(shù)組的總概率精度
//概率數(shù)組循環(huán)
foreach ($proArr as $key => $proCur) {
$randNum = mt_rand(1, $proSum);
if ($randNum <= $proCur) {
$rs = $key;
break;
} else {
$proSum -= $proCur;
}
}
unset($proArr);
return $rs;
}
$prize_arr = array(
'0' => array('id' => 10, 'title' => 'iphone5s', 'v' => 5),
'1' => array('id' => 20, 'title' => '聯(lián)系筆記本', 'v' => 10),
'2' => array('id' => 30, 'title' => '音箱設(shè)備', 'v' => 20),
'3' => array('id' => 40, 'title' => '30GU盤', 'v' => 30),
'4' => array('id' => 50, 'title' => '話費50元', 'v' => 10),
'5' => array('id' => 60, 'title' => 'iphone6s', 'v' => 15),
'6' => array('id' => 70, 'title' => '謝謝,繼續(xù)加油哦!~', 'v' => 10),
);
$arr=array();
foreach ($prize_arr as $key => $val) {
$arr[$key+1] = $val['v'];
}
echo '<pre />';
print_r($prize_arr);
print_r($arr);
//exit();
echo $prize_id = getRand($arr); //根據(jù)概率獲取獎品id
//$data['msg'] = ($prize_id == 7) ? 0 : 1; //如果為0則沒中
echo $data['prize_title'] = $prize_arr[$prize_id - 1]['title']; //中獎獎品
//echo json_encode($data);//以json數(shù)組返回給前端
exit();
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。