PHP winning probability lottery Algorithm program code _php tutorial

Source: Internet
Author: User
Tags time 0
Early on these requirements, and then according to the information on the Internet, using a stage-based extraction method, we look at the overall program: The program is completed under the thinkphp framework, using some of the framework of the class library and functions, the following I will explain each, the controller part

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

/**
*
*
* @lanfengye
*/
Class Choujiangaction extends Action {
Start time of the lottery
var $begin _time= "2012-12-25 14:00:00"; Start time 0-No Limit
The end time of the lottery
var $stop _time= "0"; End time 0-No Limit

The prize information of this prize draw must be filled in order from the big to the small, the ID is the prize, prize is the winning information, V is the probability of winning, NUM is the number of prizes.
It should be noted that the place must also contain information that does not win, 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 ' buy 1g/year space ', ' V ' =, ' num ' =>3),
' 4 ' = = Array (' id ' = = 5, ' prize ' = ' 88 ' buy 1g/year space ', ' V ' = *, ' num ' =>4),
' 5 ' = array (' id ' = = 6, ' prize ' = ' 99 ' buy 1g/Year space ', ' V ' = *, ' num ' =>10),
);


Home Display method
Public Function index () {
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 function make () {
$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, 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 Function Get_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;
}

}



?>

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.

http://www.bkjia.com/PHPjc/372543.html www.bkjia.com true http://www.bkjia.com/PHPjc/372543.html techarticle early on these needs, and then according to the information on the Internet, the use of a stage-based extraction method, we look at the overall program: The program is completed under the thinkphp framework, so ...

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