码迷,mamicode.com
首页 > Web开发 > 详细

php计算抽奖的概率

时间:2014-08-11 15:09:32      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:php计算抽奖的概率

<?php 

    //计算抽奖的概率
    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;   
    }   
    $prize_arr = array(   
        '0' => array('id'=>1,'prize'=>'1000000514','v'=>2),   
        '1' => array('id'=>2,'prize'=>'1000000513','v'=>5),   
        '2' => array('id'=>3,'prize'=>'1000000512','v'=>13),   
        '3' => array('id'=>4,'prize'=>'1000000511','v'=>15),   
        '4' => array('id'=>5,'prize'=>'1000000510','v'=>25),   
        '5' => array('id'=>6,'prize'=>'1000000509','v'=>30),  
		'6' => array('id'=>7,'prize'=>'1000000508','v'=>10), 
    );   
    foreach ($prize_arr as $key => $val) {   
        $arr[$val['id']] = $val['v'];   
    }   
    $rid = get_rand($arr);  
    $res['yes'] = $prize_arr[$rid-1]['prize']; 
    unset($prize_arr[$rid-1]);    
    shuffle($prize_arr);  
	$prize_arrcount = count($prize_arr); 
    for($i=0;$i<$prize_arrcount;$i++){   
        $pr[] = $prize_arr[$i]['prize'];   
    }   
    $res['no'] = $pr;   
	//抽奖结果
    $ro = $res['yes'];
	print_r($ro);
		


?>

php计算抽奖的概率,布布扣,bubuko.com

php计算抽奖的概率

标签:php计算抽奖的概率

原文地址:http://blog.csdn.net/phpfenghuo/article/details/38491403

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!