Php random lottery code analysis based on prize percentage

Source: Internet
Author: User
/*** Probability algorithm ** @ paramarray $ probability * @ returninteger | string */functionget_rand ($ probability) {// total probability precision of the probability array $ max = array_sum ($ probability );..."
/*** Probability algorithm ** @ param array $ probability * @ return integer | string */function get_rand ($ probability) {// total probability precision of the probability array $ max = array_sum ($ probability); foreach ($ probability as $ key => $ val) {$ rand_number = mt_rand (1, $ max); // random value from 1 to max if ($ rand_number <= $ val) {// if this value is less than or equal to the probability of the current award, we think that the winning return $ key;} else {$ max-= $ val; // otherwise, max minus the probability of the current award, then proceed to the calculation}/*** to sum up the analysis: * Make sure that the probability of each person obtaining the prize is the same * If a prize is missing, you should The probability of this lecture is changed to 0 * considering the high concurrency, after detecting that the user wins the prize, check whether the prize exists, if you don't have a prize, you can directly return the result * if you don't have a first-level prize *. then, you can return the result */} // Probability Ratio/*. Next, we will configure the award through PHP. */$ Data = array ("prize" => "tablet", "prob" => 1), array ("prize" => "digital camera ", "prob" => 3), array ("prize" => "speaker device", "prob" => 5), array ("prize" => "8g usb flash drive ", "prob" => 20), array ("prize" => "10Q coin", "prob" => 200 ), array ("prize" => "next time you may be able to win", "prob" => 771); foreach ($ data as $ key => $ val) {$ probability [$ key] = $ val ["prob"] ;}$ n = get_rand ($ probability ); $ res ['yes'] = $ data [$ n] ["prize"]; // $ res ['yes'] = $ data [$ n] [0]; unset ($ data [$ n]); // removes the Medium Award from the array, and the remaining non-Medium Award shuffle ($ data ); // disrupt the order of other awards $ func = create_function ('$ X', 'return $ x ["prize"];'); // $ res ['no'] = array_map (function ($ x) {return $ x [0] ;}, $ data ); // Other data except the winner $ res ['no'] = array_map ($ func, $ data); // Other data except the winner print_r ($ 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.