Home >Backend Development >PHP Tutorial >php lottery algorithm

php lottery algorithm

WBOY
WBOYOriginal
2016-10-10 11:56:251175browse

Do the almighty gods have any better lottery algorithm?

Reply content:

Do the almighty gods have any better lottery algorithm?

return false

This is what I wrote before. There are two popular algorithms used on the Internet, and the probability is too high. I had to go back to the textbook and just put all the winning balls and interference balls into the basket and draw them randomly.
And the lottery is drawn directly without putting it back. We only need to give out the prizes again and if the prizes are gone, we will directly give out the prizes as a thank you for participating.

<code><?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
 
 
    public function index(){
            $prize=array(
                array('id'=>1,'name'=>'苹果电脑','chance'=>1),
                array('id'=>2,'name'=>'苹果手机','chance'=>2),
                array('id'=>3,'name'=>'1000元购物卡','chance'=>3),
                array('id'=>4,'name'=>'300元餐具','chance'=>4),
                array('id'=>5,'name'=>'100元手机充值卡','chance'=>5)
            );
            $prize_form[0]=array('id'=>0,'name'=>'谢谢参与!');
            foreach ($prize as $key => $value) {
                //格式化数组
                $prize_form[$value['id']]=$value;
                if($value['chance']>0){
                    $p[$value['id']]=$value['chance'];
 
                }
                
            }
 
            $result=$this->get_rand($p);
 
            echo $prize_form[$result]['name'];
    }
 
    /*
    *奖项的ID不得为0,0默认为谢谢参与
    *$proArr=array('1'=>'1','2'=>2)
    **/
    
    private function get_rand($proArr)
    {
        //奖项数量
        $prize_number=count($proArr);
        foreach ($proArr as $key => $value) {
            
            for($i=0;$i<$value;$i++){
                //生成得奖球
                $seekGroup[]='P'.$key;
 
            }
        }
      
        $chance_sum=array_sum($proArr);
        //干扰球数量
        $disturb_number=$prize_number*100-$chance_sum;
        //生成干扰球
        $disturb=range(1,$disturb_number);
        //将球放入篮子里
        $basket=array_merge($seekGroup,$disturb);
        shuffle($basket);
        //抽奖
        $rand=rand(0,$prize_number*100-1);
        if(strstr($basket[$rand], "P")){
            $result=str_replace('P', '', $basket[$rand]);
        }else{
            $result=0;
        }
 
        return $result;
    }
 
 
 
 
 
}
?></code>

This problem is relatively easy to solve. Assume there are 100 prizes and 100 people to whom they are distributed. The 100 prizes are numbered as award0-99, and the 100 people to whom they are distributed are numbered as person0-99. The code is implemented as follows:

<code><?php
/**
 * 关注微信公众号:phpgod.
 * User: PHP技术大全
 * Date: 2016/10/9
 * Time: 9:47
 */
$list = range(0,99);
$awardList = $list;
$personList = $list;
shuffle($awardList);
shuffle($personList);

$targetAwardResult = array_combine($awardList,$personList);

var_export($targetAwardResult);</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn