PHP lottery algorithm code for winning probability. 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: The program was completed under the ThinkPHP framework, in the early stage, based on the information on the Internet, we adopted a step-by-step extraction method. let's take a look at the overall program: This program was completed under the ThinkPHP framework, some built-in class libraries and functions are used. I will explain them one by one.
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: |
|
/** * * * @ Lanfengye */ Class ChoujiangAction extends Action { // Start time of the lottery Var $ begin_time = "14:00:00"; // The start time is 0-unlimited. // Lottery end time Var $ stop_time = "0"; // The end time is 0-unlimited. // The prize information for this lottery must be filled in the ascending order, with id as the prize count, prize as the prize count, v as the probability of winning, and num as the prize count. // It must be noted that this field must also contain non-winning information, and 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 1 GB/year space Purchase ', '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 the homepage Public function index (){ // Connect to the database to obtain the list of winners $ Choujiang = M ('choujiang '); $ This-> assign ('list', $ Choujiang-> where ("rid> 0")-> order ('Id desc')-> select ()); Unset ($ Choujiang ); // The start time of the lottery is displayed on the homepage. $ This-> assign ('In in _ time', $ this-> begin_time ); $ This-> display (); } /** * Generate the winning information. ajax requests this method and the customer needs to 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, 'lottery has not started yet, start time: '. $ this-> begin_time ); Exit; } If (! Empty ($ this-> stop_time) & time ()> strtotime ($ this-> stop_time )){ $ This-> ajaxReturn (1, 'The lottery has ended, and the end time is: '. $ this-> stop_time ); Exit; } // Obtain an array of award information 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); // Obtain the award id based on the probability. $ Str = $ prize_arr [$ rid-1] ['prize']; // Medium Award $ Choujiang = M ('choujiang '); // Obtain the number of times the specified QQ number has been used up for the lottery from the database. if the value is greater than or equal to 3, 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 is obtained from the database. if the number is greater than or equal to the maximum number set, the system prompts that the prize has been exhausted. if you need to keep the last Commemorative Prize, modify the number here. } Elseif ($ Choujiang-> where ("rid = {$ rid}")-> count () >=$ prize_arr [$ rid-1] ['num']) { $ Str = 'Sorry, you have already completed 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 ); // 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.
Compile this program is completed in the ThinkPHP framework, so that...