php抽獎演算法

來源:互聯網
上載者:User
萬能的大神們有什麼比較好的抽獎演算法。

回複內容:

萬能的大神們有什麼比較好的抽獎演算法。

return false

這是我之前寫的。網上用流行的是兩種演算法,機率太高了。只好迴歸到課本裡面的,直接把所有中獎的球和幹擾球放到籃子裡面,隨機抽。
而且抽獎直接抽,無需放回,我們只要再出獎後,獎品如果沒了,就直接當成謝謝參與來出獎。

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;    }     }?>

這個問題比較好解決哈,假設有100個獎品,發放對象也有100個人,對這100個獎品進行編號叫award0-99,對發放對象的100個人進行編號叫person0-99,代碼實現如下:

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.