PHP Lottery algorithm program code sharing, lottery algorithm program code _php tutorial

Source: Internet
Author: User
Tags time 0

PHP Lottery algorithm program code sharing, 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
 
  
 */class Choujiangaction extends Action {//Start time of the sweepstakes var $begin _time= "2012-12-25 14:00:00";//Start time 0-unlimited//Draw end time VA R $stop _time= "0"; End time 0-No Limit//The prize information of this draw must be filled in order from big 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 the information of not winning, the probability from small to large to sort Var $pr Ize_arr = Array (' 0 ' = = Array (' id ' = = 1, ' prize ' = ' 44 ' buy 1g/year space ', ' V ' = 1, ' num ' =>1), ' 1 ' = Arra Y (' id ' = + 2, ' prize ' = $55 buy 1g/year space ', ' V ' = 2, ' num ' =>2), ' 2 ' = = Array (' id ' = = 3, ' Prize ' + ' 66 yuan Buy 1g/year space ', ' V ' = 5, ' num ' =>2), ' 3 ' = = Array (' id ' = ' 4 ', ' prize ' = ' 77 ' buy 1g/year space ', ' V ' + ', ' num ' =  3), ' 4 ' = = Array (' id ' = 5, ' prize ' = ' 88 yuan ' 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 Function index () {//Connect to the database to get the list of 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 draw in the homepage $this->assign (' Begin_time ', $this->begin_time);  $this->display ();    }/** * Generate the winning information, Ajax request This method, the customer needs to fill in the QQ number */Public function makes () {$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 ()
  
   
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 and the end time is: '. $this->stop_time);    Exit         }//Get an array of awards information from private members $prize _arr= $this->prize_arr;    foreach ($prize _arr as $key = + $val) {$arr [$val [' id ']] = $val [' V ']; }//$rid The winning sequence number $rid = $this->get_rand ($arr); Get the prize id $str = $prize _arr[$rid -1][' Prize '] based on probability;           The prize $Choujiang =m (' Choujiang '); Gets the number of times a particular QQ number has been drawn from the database, if it is greater than or equal to 3, the number of prompts runs out if ($Choujiang->where ("qq_no= ' {$qq _no} '")->count () >=3) {$str = ' You have run out of 3 lucky draw!      ';      $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 it to}elseif ($Choujiang->where ("rid={$rid}") Count () >= $prize _arr[$rid -1][' num ']) {$str = ' I'm sorry, the prize you have won has been finished!      ';    $rid = 0; }//Generate a user lottery data for recording to database $data =array (' Rid '= = $rid, ' pop ' = $str, ' qq_no ' + $qq _no, ' Input_time ' =>time ());    Writes the array of user sweepstakes information to the database $Choujiang->add ($data);          Unset ($Choujiang);  Ajax return Information $this->ajaxreturn (1, $STR);    /** * Get the winning number by probability */Private Function Get_rand ($PROARR) {$result = ';    The total probability accuracy of the 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; }}?>
  
 

The algorithm is simple to use, concurrent access performance is very good, a little change can be used in a variety of situations, combined with user login information can effectively control everyone's draw times. Changing the start and end to an array will make it perfect for a particular time of day to draw.

I hope you can combine the article about the PHP lottery program to learn more, better grasp the application of the language skills.

http://www.bkjia.com/PHPjc/1058145.html www.bkjia.com true http://www.bkjia.com/PHPjc/1058145.html techarticle PHP Lottery Algorithm program code sharing, lottery algorithm program code lottery algorithm needs to meet the requirements are as follows: 1. Can control the probability of winning 2. With randomness 3. It is best to control the number of prizes ...

  • 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.