Home>

### PHP Lottery Algorithm Program Code Sharing

The requirements that the lottery algorithm needs to meet are as follows:

1. Can control the probability of winning

2. Random

3. It is best to control the number of prizes

4. Limit the number of draws based on user id or ip, mobile phone number, qq number and other conditions

These requirements were in the early stages,Then based on the information online,Adopted a phased extraction method,Let's take a look at the overall process:

The program is completed under thinkphp framework,Using some of the framework's class libraries and functions,I will explain them one by one below.Controller part:

code show as below

``````<?php
/**
*
*
* @lanfengye<[email protected]>
* /
class choujiangaction extends action {
//The start time of the draw
var \$begin_time="2012-12-25 14:00:00";//start time 0-unlimited
//End time of the draw
var \$stop_time="0";//end time 0-unlimited
//The prize information of this lottery,Must be filled in descending order,id is the prize time, prize is the winning information,v is the probability of winning,num is the number of prizes
//have to be aware of is,The place must also contain information that the prize has not been won.
Sorting Probabilities from Small to Large
var \$prize_arr=array (
"0" =>array ("id" =>1, "prize" =>"44 yuan for 1g/year of space", "v" =>1, "num" =>1),    "1" =>array ("id" =>2, "prize" =>"55 yuan to buy 1g/year space", "v" =>2, "num" =>2),    "2" =>array ("id" =>3, "prize" =>"66 yuan to buy 1g/year space", "v" =>5, "num" =>2),    "3" =>array ("id" =>4, "prize" =>"77 yuan to buy 1g/year space", "v" =>10, "num" =>3),    "4" =>array ("id" =>5, "prize" =>"88 yuan to buy 1g/year space", "v" =>15, "num" =>4),    "5" =>array ("id" =>6, "prize" =>"99 yuan to buy 1g/year space", "v" =>67, "num" =>10),  );
//Homepage display method
public function index () {
//Connect to the database,The list of people who won the prize
\$choujiang=m ("choujiang");
\$this->assign ("list", \$choujiang->where ("rid>0")->order ("id desc")->select ());
unset (\$choujiang);
//Show the start time of the draw on the homepage
\$this->assign ("begin_time", \$this-&begin_time);
\$this->display ();
}
/**
* Generate winning information,ajax requests this method,Require customers to fill in 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 qq number correctly!");
exit;
}
if (! empty (\$this->begin_time)&&time ()<strtotime (\$this->begin_time)) {
\$this->ajaxreturn (1, "The draw has not yet started,Start time:". \$This->begin_time);
exit;
}
if (! empty (\$this-&stop;time_time)&&time ()>strtotime (\$this-&stop;time_time)) {
\$this->ajaxreturn (1, "This draw has ended.
End time:". \$This->stop_time);
exit;
}
//Get the award information array,From private members
\$prize_arr=\$this->prize_arr;
foreach (\$prize_arr as \$key =>\$val) {
\$arr [\$val ["id"]]=\$val ["v"];
}
//\$rid winning serial number
\$rid=\$this->get_rand (\$arr);//Get award id based on probability
\$str=\$prize_arr [\$rid-1] ["prize"];//Mid Prize
\$choujiang=m ("choujiang");
//Get the number of times that a specific qq number has participated in the draw from the database,If it is greater than or equal to 3, the number of prompts is used up
if (\$choujiang->where ("qq_no =" {\$qq_no} "")->count ()>= 3) {
\$str="You have run out of 3 lucky draw chances!";
\$rid=0;
//The number of times a particular award serial number was obtained from the database,If it is greater than or equal to the maximum number of times set, the prize will be drawn out.
If you need to win the last commemorative prize,Then modify it
} elseif (\$choujiang->where ("rid={\$rid}")->count ()>= \$prize_arr [\$rid-1] ["num"]) {
\$str="Sorry, the prize you have drawn has already been won!";
\$rid=0;
}
//Generate data for a user's draw,Used to log to the database
\$data=array (
"rid" =>\$rid,      "pop" =>\$str,      "qq_no" =>\$qq_no,      "input_time" =>time ()
);
//Write user lottery information array to database
unset (\$choujiang);
//ajax return information
\$this->ajaxreturn (1, \$str);
}
/**
* Winning numbers based on probability
* /
private function get_rand (\$proarr) {
\$result="";
//Total probability precision 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,It can be used for a variety of occasions with a little modification,Combining information such as user login can effectively control the number of draws for each person.Change between start and end to an array,You can complete the process of drawing a lottery at a specific time every day.

• Previous How an APK program obtains system permissions
• Next Java implementation of excel table