Share a PHP lottery algorithm program code

Source: Internet
Author: User

Share a PHP lottery algorithm program code

The lottery algorithm needs to meet the following requirements:

1. Can control the probability of winning

2. Have randomness

3. It is best to control the number of prizes

4. According to the user ID or IP, mobile phone number, QQ number and other conditions limit draw number

Early on these needs, and then according to the information on the Internet, using a phase-based extraction method, we look at the overall program:

The program is done under the thinkphp framework, using some of the framework's own class libraries and functions, I will explain the controller section:

The code is as follows

<?PHP/** * * * * @lanfengye <[email protected]>*/classChoujiangactionextendsAction {//start time of the lottery  var $begin _time= "2012-12-25 14:00:00";//start time 0-No limit//Draw end time  var $stop _time= "0";//End time 0-No Limit//The prize information of this lottery must be filled in order from large to small, ID is prize, prize is winning information, V is the probability of winning, NUM is the prize number//need to be noted, the place also must contain not winning information, the probability from small to large to sort   var $prize _arr=Array(    ' 0 ' + =Array(' id ' = 1, ' prize ' = ' $44 ' buy 1g/year space ', ' V ' = 1, ' num ' =>1), ' 1 ' =Array(' id ' = 2, ' prize ' = ' $55 ' buy 1g/Year space ', ' V ' = 2, ' num ' =>2), ' 2 ' =Array(' id ' = 3, ' prize ' = ' $66 ' buy 1g/year space ', ' V ' = 5, ' num ' =>2), ' 3 ' =Array(' id ' = 4, ' prize ' = ' $77 ' to buy 1g/year space ', ' V ' = ', ' num ' =>3 '), ' 4 ' =Array(' id ' = 5, ' prize ' = ' $88 ' to buy 1g/year space ', ' V ' = +, ' num ' =>4), ' 5 ' =Array(' id ' = 6, ' prize ' = ' $99 ' to buy 1g/year space ', ' V ' = *, ' num ' =>10),  ); //Home Display Method   Public functionindex () {//connect to the database to get a list of the people who won the lottery    $Choujiang=m (' Choujiang '); $this->assign (' list ',$Choujiang->where ("rid>0")->order (' id desc ')Select ()); unset($Choujiang); //show the start time of the lottery in the home page    $this->assign (' Begin_time ',$this-begin_time); $this-display (); }            /** * Generate winning information, Ajax request this method, customers need to fill in the QQ number*/   Public functionMake () {$qq _no=Trim($_post[' Qq_no ']); Import (' ORG. Util.input '); $qq _no=input::getvar ($qq _no); if(Empty($qq _no)){      $this->ajaxreturn (1, ' Please fill in the correct QQ number! ‘); Exit; }          if(!Empty($this->begin_time) && Time() <Strtotime($this-begin_time)) {      $this->ajaxreturn (1, ' The lottery has not started, the start time is: '.$this-begin_time); Exit; }          if(!Empty($this->stop_time) && Time() >Strtotime($this-stop_time)) {      $this->ajaxreturn (1, ' This draw is over, the end time is: '.$this-stop_time); Exit; }           //get an array of prize information from a private member    $prize _arr=$this-Prize_arr; foreach($prize _arr  as $key=$val) {      $arr[$val[' ID ']] =$val[' V ']; }    //$rid The winning serial number    $rid=$this->get_rand ($arr);//get the prize ID based on probability          $str=$prize _arr[$rid-1][' prize ';//Chinese Awards          $Choujiang=m (' Choujiang '); //gets the number of times a particular QQ number has been drawn from the database, and if it is greater than or equal to 3, the number of prompts is exhausted    if($Choujiang->where ("qq_no= ' {$qq _no} ').Count() >=3){      $str= ' You have run out of 3 draws! ‘; $rid=0; //The number of times a particular prize number is obtained from the database, greater than or equal to the maximum number of times the prize is drawn, and if the last commemorative prize is required, modify the place}ElseIf($Choujiang->where ("rid={$rid} ")Count() >=$prize _arr[$rid-1][' num ']) {      $str= ' I'm sorry, the prize you have won is finished! ‘; $rid=0; }    //generate a user lottery data for recording to the database    $data=Array(      ' Rid ' =$rid, ' pop ' =$str, ' qq_no ' =$qq _no, ' input_time ' = Time()    ); //writes an array of user sweepstakes information to the database          $Choujiang->add ($data); unset($Choujiang); //Ajax return Information    $this->ajaxreturn (1,$str); }      /** * Get winning numbers based on probabilities*/  Private functionGet_rand ($PROARR) {    $result= ' '; //the total probability accuracy of probability array    $proSum=Array_sum($PROARR); //probability array Loop    foreach($PROARR  as $key=$proCur) {      $randNum=Mt_rand(1,$proSum); if($randNum<=$proCur) {        $result=$key;  Break; } Else {        $proSum-=$proCur; }    }    unset($PROARR); return $result; }    }   ?>

Share a PHP lottery algorithm program code

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.