This article mainly shares the PHP lottery algorithm program design, which we will see in our daily lives. if you need to know how to implement the lucky draw algorithm using php, refer to it.
The lottery algorithm must meet the following requirements:
1. you can control the probability of winning a prize
2. Random
3. it is best to control the number of prizes
4. restrict the number of lottery draws based on user ID, ip address, mobile phone number, QQ number, and other conditions
In the early stage, based on the online data, we adopted a step-by-step extraction method. let's take a look at the overall program below:
This program is completed in the ThinkPHP framework. some built-in class libraries and functions are used. I will describe them one by one, Controller part:
The code is as follows:
<? Php/***** @ lanfengye
*/Class ChoujiangAction extends Action {// lottery start time var $ begin_time = "14:00:00 "; // start time 0-unlimited // The draw end time var $ stop_time = "0"; // The draw end time 0-unlimited // The Lottery award information, you must fill in the order from large to small. id is the prize, prize is the winning information, v is the winning probability, and num is the number of prizes. // note that, this field must also contain non-winning information. the probability is sorted from small to large. var $ prize_arr = array ('0' => array ('id' => 1, 'prize' => '44 RMB 1G/year space Purchase ', 'V' => 1, 'num' => 1 ), '1' => array ('id' => 2, 'prize' => 'purchase 1G/year space for CNY 55 ', 'V' => 2, 'num' => 2), '2' => array ('id' => 3, 'prize' => 'Buy 1G/year space for 66 yuan ', 'V' => 5, 'num' => 2), '3' => array ('id' => 4, 'prize' => '77 RMB for 1 GB/year ', 'V' => 10, 'num' => 3), '4' => array ('id' => 5, 'prize' => '88 yuan 1G/year space Purchase ', 'V' => 15, 'num' => 4 ), '5' => array ('id' => 6, 'prize' => '99 RMB 1G/year space Purchase ', 'V' => 67, 'num' => 10),); // display method on the homepage public function index () {// connect to the database, obtain the list of winners $ Choujiang = M ('choujiang '); $ this-> assign ('list', $ Chou Jiang-> where ("rid> 0")-> order ('Id desc')-> select (); unset ($ Choujiang ); // display the lottery start time on the homepage $ this-> assign ('In in _ time', $ this-> begin_time); $ this-> display ();} /*** generate the winning information. ajax requests this method. you must enter 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,' enter the correct QQ number! '); Exit;} if (! Empty ($ this-> begin_time) & time ()
Begin_time) {$ this-> ajaxReturn (1, 'Drawing hasn't started yet, start time: '. $ this-> begin_time); exit;} if (! Empty ($ this-> stop_time) & time ()> strtotime ($ this-> stop_time) {$ this-> ajaxReturn (1, 'this lottery has ended, end time :'. $ this-> stop_time); exit;} // an array of award information, from private member $ 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 ); // Obtain the award id $ str = $ prize_arr [$ rid-1] ['prize'] based on the probability; // Medium Award $ Choujiang = M ('choujiang '); // Obtain the number of times the specified QQ number has been used in the lottery from the database If the value is greater than or equal to 3, the system prompts that the number of times is used up. if ($ Choujiang-> where ("qq_no = '{$ qq_no}'")-> count ()> = 3) {$ str = 'You have used up three lucky draws! '; $ Rid = 0; // The number of times that a specific award sequence number is obtained from the database. if the number is greater than or equal to the maximum number of times set, the system prompts that the prize has been exhausted. if you need to keep winning the last Commemorative Award, modify this field to} elseif ($ Choujiang-> where ("rid = {$ rid}")-> count ()> = $ prize_arr [$ rid-1] ['num']) {$ str = 'Sorry, you have already won the award! '; $ Rid = 0 ;}// generate a user lottery data to record to the database $ data = array ('RID' => $ rid, 'pop' => $ str, 'qq _ no' => $ qq_no, 'input _ time' => time ()); // write the user lottery information array to the database $ Choujiang-> add ($ data); unset ($ Choujiang); // ajax return information $ this-> ajaxReturn (1, $ str);}/*** obtain the winning number based on the probability */private function get_rand ($ proArr) {$ result = ''; // The total probability precision of the probability array $ proSum = array_sum ($ proArr); // The 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 and has excellent concurrent access performance. it can be used for various occasions with slight changes. combined with user logon and other information, the number of lottery draws can be effectively controlled for each user. Change the period between start and end to an array to complete the lottery program at a specific time every day.
I hope you can learn more about the php lottery program and better understand the application skills of this language.