PHP lottery program probability algorithm-PHP source code

Source: Internet
Author: User
PHP lottery program probability algorithm jump

// Probability algorithm, 6 awards $ prize_arr = array ('0' => array ('id' => 1, 'prize' => 'iphone6 ', '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' => '50q coin ', 'V' => 24 ), '4' => array ('id' => 5, 'prize' => '10q coin ', 'V' => 60 ), '5' => array ('id' => 6, 'prize' => '1q coin ', 'V' => 1900 ),); // the chance of winning each prize. The prize ID is used as the array subscript foreach ($ prize_arr as $ val) {$ item [$ val ['id'] = $ val ['V'];} function get ($ item) {// Winning probability base $ num = array_sum ($ item); // The probability of the current first prize is 1/2000 foreach ($ item as $ k => $ v) {// Obtain a random number from 1 to the current base range $ rand = mt_rand (1, $ num); if ($ rand <= $ v) {// assume that the current award $ k = 2, $ v1900 won't win the sixth prize. The total prize base is 2000-1900, in none of the first five cycles, 2000-1-5-10-24-60 = 1900, etc., haha $ num-= $ v ;}} return $ res;} $ res = get ($ item); $ h = $ prize_arr [$ res-1] ['prize']; echo $ h. ';';

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.