Php winning probability algorithm and winning probability algorithm

Source: Internet
Author: User

Php winning probability algorithm and winning probability algorithm

We will first complete the PHP process in the background. The main task of PHP is to configure the awards and the corresponding winning probability. When the front-end page clicks a box, we will want PHP to send ajax requests in the background, then, based on the configuration probability, the backend PHP uses the probability algorithm to give the winning result, and sends the unwon award information to the front-end page in JSON format.

Let's look at the probability calculation function.

1 function get_rand ($ proArr) {2 $ result = ''; 3 4 // The total probability accuracy of the probability array is 5 $ proSum = array_sum ($ proArr ); 6 7 // probability array loop 8 foreach ($ proArr as $ key => $ proCur) {9 $ randNum = mt_rand (1, $ proSum ); 10 if ($ randNum <= $ proCur) {11 $ result = $ key; 12 break; 13} else {14 $ proSum-= $ proCur; 15} 16} 17 unset ($ proArr); 18 19 return $ result; 20}

 

The above code is a classic probability algorithm. $ proArr is a preset array. Assume that the array is: array (100,200,300,400 ), in the beginning, the probability range is used to determine whether the first number is within the probability range of occurrence. If not, the probability space is used, 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.

1 $ prize_arr = array (2 '0' => array ('id' => 1, 'prize' => 'tablet ', 'V' => 1 ), 3 '1' => array ('id' => 2, 'prize' => 'digital camera ', 'V' => 5 ), 4 '2' => array ('id' => 3, 'prize' => 'speaker device', 'V' => 10 ), 5 '3' => array ('id' => 4, 'prize' => '4g disks', 'V' => 12 ), 6 '4' => array ('id' => 5, 'prize' => '10q coin ', 'V' => 22 ), 7 '5' => array ('id' => 6, 'prize' => 'next time you may be able to get in,', 'V' => 50), 8 );

The two-dimensional array records all the prize information for this lottery. The id indicates the prize level, the prize indicates the prize, and the v indicates the probability of the prize. 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.

1 foreach ($ prize_arr as $ key => $ val) {2 $ arr [$ val ['id'] = $ val ['V']; 3} 4 5 $ rid = get_rand ($ arr ); // obtain the award id 6 7 $ res ['yes'] = $ prize_arr [$ rid-1] ['prize'] based on the probability. // medium award 8 unset ($ prize_arr [$ rid-1]); // removes medium Award from the array, and 9 shuffle ($ prize_arr) is left ); // disrupt the array Order 10 for ($ I = 0; $ I <count ($ prize_arr); $ I ++) {11 $ pr [] = $ prize_arr [$ I] ['prize']; 12} 13 $ res ['no'] = $ pr; 14 echo json_encode ($ res );

 

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.