首页 >php教程 >PHP源码 >抽奖概率 PHP

抽奖概率 PHP

PHP中文网
PHP中文网原创
2016-05-26 08:21:021252浏览

抽奖概率

<?php 

function get_rand($proArr) { 
    $result = &#39;&#39;; 

    //概率数组的总概率精度
    $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; 
}

$prize_arr = array( 
    &#39;0&#39; => array(&#39;id&#39;=>1,&#39;prize&#39;=>&#39;平板电脑&#39;,&#39;v&#39;=>0), 
    &#39;1&#39; => array(&#39;id&#39;=>2,&#39;prize&#39;=>&#39;数码相机&#39;,&#39;v&#39;=>0), 
    &#39;2&#39; => array(&#39;id&#39;=>3,&#39;prize&#39;=>&#39;音箱设备&#39;,&#39;v&#39;=>0), 
    &#39;3&#39; => array(&#39;id&#39;=>4,&#39;prize&#39;=>&#39;4G优盘&#39;,&#39;v&#39;=>0), 
    &#39;4&#39; => array(&#39;id&#39;=>5,&#39;prize&#39;=>&#39;10Q币&#39;,&#39;v&#39;=>1), 
    &#39;5&#39; => array(&#39;id&#39;=>6,&#39;prize&#39;=>&#39;下次没准就能中哦&#39;,&#39;v&#39;=>100), 
);  


//code here eg:unset($prize_arr[&#39;0&#39;])
foreach ($prize_arr as $key => $val) { 
    $arr[$val[&#39;id&#39;]] = $val[&#39;v&#39;]; 
} 

$rid = get_rand($arr); //根据概率获取奖项id

$res[&#39;yes&#39;] = $prize_arr[$rid-1][&#39;prize&#39;]; //中奖项
//将中奖项从数组中剔除,剩下未中奖项,如果是数据库验证,这里可以省掉
unset($prize_arr[$rid-1]); 
shuffle($prize_arr); //打乱数组顺序
for($i=0;$i<count($prize_arr);$i++){ 
    $pr[] = $prize_arr[$i][&#39;prize&#39;]; 
} 
$res[&#39;no&#39;] = $pr; 
echo "<pre class="brush:php;toolbar:false">";
print_r($res);
// echo json_encode($res);

2. [代码]抽奖概率

<?php 

function get_rand($proArr) { 
    $result = &#39;&#39;; 

    //概率数组的总概率精度
    $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; 
}

$prize_arr = array( 
    &#39;0&#39; => array(&#39;id&#39;=>1,&#39;prize&#39;=>&#39;平板电脑&#39;,&#39;v&#39;=>0), 
    &#39;1&#39; => array(&#39;id&#39;=>2,&#39;prize&#39;=>&#39;数码相机&#39;,&#39;v&#39;=>0), 
    &#39;2&#39; => array(&#39;id&#39;=>3,&#39;prize&#39;=>&#39;音箱设备&#39;,&#39;v&#39;=>0), 
    &#39;3&#39; => array(&#39;id&#39;=>4,&#39;prize&#39;=>&#39;4G优盘&#39;,&#39;v&#39;=>0), 
    &#39;4&#39; => array(&#39;id&#39;=>5,&#39;prize&#39;=>&#39;10Q币&#39;,&#39;v&#39;=>1), 
    &#39;5&#39; => array(&#39;id&#39;=>6,&#39;prize&#39;=>&#39;下次没准就能中哦&#39;,&#39;v&#39;=>100), 
);  


//code here eg:unset($prize_arr[&#39;0&#39;])
foreach ($prize_arr as $key => $val) { 
    $arr[$val[&#39;id&#39;]] = $val[&#39;v&#39;]; 
} 

$rid = get_rand($arr); //根据概率获取奖项id

$res[&#39;yes&#39;] = $prize_arr[$rid-1][&#39;prize&#39;]; //中奖项
//将中奖项从数组中剔除,剩下未中奖项,如果是数据库验证,这里可以省掉
unset($prize_arr[$rid-1]); 
shuffle($prize_arr); //打乱数组顺序
for($i=0;$i<count($prize_arr);$i++){ 
    $pr[] = $prize_arr[$i][&#39;prize&#39;]; 
} 
$res[&#39;no&#39;] = $pr; 
echo "<pre class="brush:php;toolbar:false">";
print_r($res);
// echo json_encode($res);


 以上就是抽奖概率的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn