PHP中奖概率的抽奖算法程序代码
<?php
//id为奖次,prize为中奖信息,v为中奖概率
‘0‘ => array(‘id‘ => 1, ‘prize‘ => ‘1 ‘, ‘v‘ => 1),
‘1‘ => array(‘id‘ => 2, ‘prize‘ => ‘2 ‘, ‘v‘ => 2),
‘2‘ => array(‘id‘ => 3, ‘prize‘ => ‘3 ‘, ‘v‘ => 5),
‘3‘ => array(‘id‘ => 4, ‘prize‘ => ‘4 ‘, ‘v‘ => 10),
‘4‘ => array(‘id‘ => 5, ‘prize‘ => ‘5 ‘, ‘v‘ => 15),
‘5‘ => array(‘id‘ => 6, ‘prize‘ => ‘6 ‘, ‘v‘ => 67),
);
foreach ($prize_arr as $key => $val)
{
$arr[$val[‘id‘]] = $val[‘v‘];
}
$rid = get_rand($arr); //根据概率获取奖项id
switch($rid)
{
case $rid==1:one();break;
case $rid==2:two();break;
case $rid==3:three();break;
case $rid==4:four();break;
case $rid==5:five();break;
case $rid==6:six();break;
}
/**
* 根据概率获取中奖号码
*/
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;
}
function one()
{
echo ‘one------1‘;
}
function two()
{
echo ‘two------2‘;
}
function three()
{
echo ‘three------3‘;
}
function four()
{
echo ‘four------4‘;
}
function five()
{
echo ‘five------5‘;
}
function six()
{
echo ‘six------6‘;
}
?>
本文出自 “8468118” 博客,请务必保留此出处http://8478118.blog.51cto.com/8468118/1358629
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。