Rumah  >  Artikel  >  php教程  >  抽奖概率 PHP

抽奖概率 PHP

PHP中文网
PHP中文网asal
2016-05-26 08:21:021228semak imbas

抽奖概率

<?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)!


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:因数与完全数Artikel seterusnya:生成要排除的号码