PHP Random Draw Example Analysis

Source: Internet
Author: User

<?PHPHeader(' Content-type:text/html;charset=utf-8 ');/** * Lottery Tools*/classLottery_tool {protected Static $AWARDSARR; protected Static $proField= ' probability '; protected Static $proSum= 0; protected Static $checkAward=false; ConstSuccess_code = 0; ConstFail_code =-1; //Check Lottery Data    protected Static functioncheckawards () {if(!Is_array(Self::$AWARDSARR) ||Empty(Self::$AWARDSARR)) {            returnSelf::$checkAward=false; } Self::$proSum= 0; foreach(Self::$AWARDSARR  as $_key=$award) { self::$proSum+=$award[Self::$proField]; }        if(Empty(Self::$proSum)) {            returnSelf::$checkAward=false; }        returnSelf::$checkAward=true; }    protected Static functionSuccessroll ($rollKey){        return Array(' Code ' = Self::success_code, ' roll_key ' =$rollKey, ' msg ' = ' roll success '); }    protected Static functionFailroll ($msg= ' Roll fail '){        return Array(' Code ' = Self::fail_code, ' msg ' =$msg ); }    //Lottery     Public Static functionRoll () {if(false= = Self::$checkAward) {            returnSelf::failroll (' awards data is not the right format! '); }        $result=Mt_rand(0, Self::$proSum); $proValue= 0; foreach(Self::$AWARDSARR  as $_key=$value) {            $proValue+=$value[Self::$proField]; if($result<=$proValue) {                returnSelf::successroll ($_key); }        }        returnSelf::failroll (' wrong '); }     //Change the probability field name     Public Static functionSetprofield ($field=NULL) {        if(!Empty($field) ) { self::$proField=$field; }    }    //Set up Prizes     Public Static functionSetawards ($awards) { self::$AWARDSARR=$awards; Self::checkawards (); }}$awards=Array(        ' 0 ' + =Array(' Pro ' = 1, ' info ' = ' 1% possibilities ', ' num ' =>0), ' 1 ' =Array(' pro ' = +, ' info ' = ' 25% possibilities ', ' num ' =>0), ' 2 ' =Array(' pro ' = +, ' info ' = ' 40% possibilities ', ' num ' =>0), ' 3 ' =Array(' pro ' = +, ' info ' = ' 34% possibilities ', ' num ' =>0),    ); //Change the probability field nameLottery_tool::setprofield (' Pro ');//Set up PrizesLottery_tool::setawards ($awards);$result=Array(); for($i= 100;$i--;) {     //Lottery    $result[] = Lottery_tool::Roll ();}foreach($result  as $key=$value) {    $awards[$value[' Roll_key '] [' num '] + +;}Echo' <pre> ';Var_dump($awards);

PHP Random Draw Example Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.