PHP probability algorithm instance for lottery programs and random advertisements _ PHP Tutorial

Source: Internet
Author: User
A php probability algorithm instance applicable to lottery programs and random advertisements. Then we will inevitably design algorithms in the program, that is, to give the user a prize based on a certain probability. Let's look at two probability algorithm functions. Algorithm 1: Copy the code as follows: *** the algorithm is designed in the program, that is, the user is given a prize based on a certain probability. Let's look at two probability algorithm functions.

Algorithm 1

The code is as follows:


/**
* Full probability calculation
*
* @ Param array $ p array ('a' => 0.5, 'B' => 0.2, 'C' => 0.4)
* @ Return string returns the key of the preceding array.
*/
Function random ($ ps ){
Static $ arr = array ();
$ Key = md5 (serialize ($ ps ));

If (! Isset ($ arr [$ key]) {
$ Max = array_sum ($ ps );
Foreach ($ ps as $ k => $ v ){
$ V = $ v/$ max * 10000;
For ($ I = 0; $ I <$ v; $ I ++) $ arr [$ key] [] = $ k;
}
}
Return $ arr [$ key] [mt_rand (0, count ($ arr [$ key])-1)];
}


Algorithm 2

The code is as follows:

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;
}


The above code is a classic probability algorithm. $ proArr is a preset array. assume that the array is: array (100,200,300,400 ), at the beginning, the probability range is used to determine whether the first number is within the probability range of occurrence. if not, the probability is null, that is, the probability space of the value of k minus the number just now. In this example, we subtract 100, that is, the second number is filtered within the range of 1,900. In this way, there will always be a number that meet the requirements. It is equivalent to touching something in a box. The first one is not, the second is not, and the third is not. The last one must be. This algorithm is simple and highly efficient. The key is that it has been used in our previous projects, especially in projects with large data volumes.
Next we will use the PHP configuration award.

The code is as follows:


$ Prize_arr = array (
'0' => array ('id' => 1, 'prize' => 'tablet PC', 'V' => 1 ),
'1' => array ('id' => 2, 'prize' => 'digital camera ', 'V' => 5 ),
'2' => array ('id' => 3, 'prize' => 'Speaker device', 'V' => 10 ),
'3' => array ('id' => 4, 'prize' => '4G disks', 'V' => 12 ),
'4' => array ('id' => 5, 'prize' => '10q coin ', 'V' => 22 ),
'5' => array ('id' => 6, 'prize' => 'Next time you may be able to get in, ', 'V' => 50 ),
);


It is a two-dimensional array that records all the prize information for this lottery. the id indicates the winning level, the prize indicates the prize, and the v indicates the winning probability. Note that the value of v must be an integer. you can set the value of v to 0, which means that the probability of the award is 0, and the sum (base) of the value of v in the array ), the larger the base, the more accurate the probability is. In this example, the sum of v is 100, and the probability of winning a tablet is 1%. if the sum of v is 10000, the probability of winning a tablet is one in ten.
Each request on the front-end page sets an array of PHP loop awards. the probability calculation function get_rand is used to obtain the prize id. Save the prize to the array $ res ['yes'], and save the remaining unwon information in $ res ['no, finally, the number of json data is output to the front-end page.

The code is as follows:

// If the winning data is stored in the database, you need to determine the number of winners.
// For medium 1, 2, and 3 prizes, if the maximum number of prizes is reached, unset the corresponding prizes to avoid repeated Awards
// Code here eg: unset ($ prize_arr ['0'])
Foreach ($ prize_arr as $ key => $ val ){
$ Arr [$ val ['id'] = $ val ['V'];
}

$ Rid = get_rand ($ arr); // Obtain the award id based on the probability.

$ Res ['yes'] = $ prize_arr [$ rid-1] ['prize']; // Medium Award
// Remove the Medium Award from the array, and the remaining ones are not. if it is a database verification, you can save it here.
Unset ($ prize_arr [$ rid-1]);
Shuffle ($ prize_arr); // disrupt the array order
For ($ I = 0; $ I $ Pr [] = $ prize_arr [$ I] ['prize'];
}
$ Res ['no'] = $ pr;
Echo json_encode ($ res );

Why can't I win the prize?

In many similar lottery activities, participants often fail to receive the grand prize. from the perspective of the program, I will give you an example. if I am the organizer of the lucky draw, I have set 6 awards, each award has a different probability of winning. if the first prize is a high-speed car, but I set the probability to 0, what does this mean? This means that no matter how the winners smoke, they will never get the car. When the organizer switches the remaining squares, the participants may find that the first prize may be under a number next to the square just drawn, and all of them blame themselves for their bad luck. Is it really luck? In fact, when the participants flip the square, the program has decided on the medium Award. the award displayed in other squares is only a smoke bullet, confusing the audience and participants. I think after reading this article, you may be tired of having to draw a lottery on the TV program. you may not be able to choose a two-color ball.

Bytes. Let's look at two probability algorithm functions. The algorithm code is as follows:/*** all...

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.