PHP Lottery Algorithm program code sharing _php Example

Source: Internet
Author: User
Tags time 0

The requirements for the

lottery algorithm are as follows:
1. Can control the probability of winning the lottery
2. With Randomness
3. It is best to control the number of prizes
4. According to the user ID or IP, mobile number, QQ number and other conditions to limit the number of lottery
Early on these requirements, then according to the information on the Internet, using a phased extraction method, we look at the whole of the program:
The program is completed in the framework of the thinkphp, the use of some of the framework of the class library and functions, I will explain each one, the Controller section:
code as follows

<?php/** * * * @lanfengye <zibin_5257@163.com>/class Choujiangaction extends Action {//Draw start time var $begin _time= "2012-12-25 14:00:00"; Start time 0-No limit//Draw end time Var $stop _time= "0"; End time 0-do not limit//This lottery award information, must be in accordance with the order from large to small to fill in, ID for the award, prize for the winning information, V for the winning probability, Num for the number of prizes//need to note that the Department must also contain the information not winning, probability from small to large to sort Var  $prize _arr = Array (' 0 ' => array (' ID ' => 1, ' Prize ' => ' 44 yuan purchase 1g/year space ', ' V ' => 1, ' num ' =>1), ' 1 ' => Array (' ID ' => 2, ' Prize ' => ' 55 yuan buy 1g/year space ', ' V ' => 2, ' num ' =>2), ' 2 ' => array (' ID ' => 3, ' Prize ' =&gt ; ' 66 yuan purchase 1g/space ', ' V ' => 5, ' num ' =>2 ', ' 3 ' => array (' ID ' => 4, ' Prize ' => ' 77 yuan purchase 1g/year space ', ' V ' =>, ' num ' =>3), ' 4 ' => array (' ID ' => 5, ' Prize ' => ' 88 yuan purchase 1g/year space ', ' V ' =>, ' num ' =>4), ' 5 ' => array (' ID
   
 
  ' => 6, ' Prize ' => ' 99 yuan buy 1g/year space ', ' V ' =>, ' num ' =>10);
    The first page shows the method public Function index () {//connection database, to obtain the list of the winners $Choujiang =m (' Choujiang ');$this->assign (' list ', $Choujiang->where ("rid>0")->order (' id desc ')->select ());
     
    Unset ($Choujiang);
     
    The start time of the lottery is shown on the home page $this->assign (' Begin_time ', $this->begin_time);
  $this->display ();
    /** * Generate winning information, Ajax to request this method, customers need to fill out QQ number/Public function made () {$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 QQ number correctly!
      ');
    Exit } if (!empty ($this->begin_time) && time () <strtotime ($this->begin_time)) {$this->ajaxret
      Urn (1, ' Lottery has not yet begun, start time: '. $this->begin_time);
    Exit } if (!empty ($this->stop_time) && time () >strtotime ($this->stop_time)) {$this->ajaxretur
      N (1, ' This draw has ended, 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 winning sequence number $rid = $this->get_rand ($arr); According to the probability to obtain the award id $STR = $prize _arr[$rid -1][' Prize '];
     
      The award $Choujiang =m (' Choujiang '); To get a specific QQ number from the database has already participated in the lottery, if greater than or equal to 3, then the number of prompts is run out if ($Choujiang->where ("qq_no= ' {$qq} ')->count () >=3) {$str = ' you 3 times We've run out of lottery opportunities!
      ';
      $rid = 0; The number of times a specific award number is obtained from the database, greater than or equal to the maximum number of settings, which indicates that the prize is drawn out, and if it is necessary to have the last commemorative award, then the modification of the place can be}elseif ($Choujiang->where ("rid={$rid}")-> Count () >= $prize _arr[$rid -1][' num ']) {$str = ' I'm sorry, the award you've drawn has already finished!
      ';
    $rid = 0;
      //Generate a user lottery data for logging to the database $data =array (' Rid ' => $rid, ' Pop ' => $str, ' qq_no ' => $qq _no,
    ' Input_time ' =>time ());
    The User lottery Information array is written to the database $Choujiang->add ($data);
     
     Unset ($Choujiang);
  Ajax return Information $this->ajaxreturn (1, $STR);
    /** * According to the probability to obtain the winning number/private function Get_rand ($PROARR) {$result = '; The total probability precision $proSum of probability array = 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;
 }}?>

This algorithm is simple to use, concurrent access performance is very good, a little change can be used in various occasions, combined with user login information can effectively control the number of lottery. By changing the start and end to an array, you can refine the process of drawing for a specific time of day.

I hope you can combine the PHP lottery program for in-depth study of the article to better grasp the language of the application techniques.

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.