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

抽奖概率 PHP

PHP中文网
PHP中文网원래의
2016-05-26 08:21:021275검색

抽奖概率

<?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으로 문의하세요.
이전 기사:因数与完全数다음 기사:生成要排除的号码