PHP winning probability algorithm, can be used to scrape cards, large turntable and other lottery algorithm

Source: Internet
Author: User
PHP winning probability algorithm, can be used for scraping cards, large turntable and other lottery algorithm

PHP winning probability algorithm, can be used for scraping cards, large turntable and other lottery algorithm. The usage is very simple, the code has the detailed comment explanation, can understand at a glance

  $proCur) {$randNum = Mt_rand (1, $proSum);             if ($randNum <= $proCur) {$result = $key;         Break         } else {$proSum-= $proCur;      }} unset ($PROARR); return $result; }/* * Awards Array * is a two-dimensional array that records all of the prize information for this draw, * where the ID indicates the winning level, prize represents the prize, and V indicates the probability of winning. * Note that the V must be an integer, and you can set the V of the corresponding prize to 0, meaning that the odds of the prize being pumped are 0, * the sum of V in the array (cardinality), the larger the cardinality, the more accurate the probability. * The sum of V in this example is 100, then the probability of winning the tablet corresponds to 1%, * if the sum of V is 10000, then the probability of winning is one out of 10,000. * */$prize _arr = Array (' 0 ' = = Array (' ID ' =>1, ' prize ' = ' tablet ', ' V ' =>1), ' 1 ' = = Array (' ID ' =>2, ' pri Ze ' + ' digital camera ', ' V ' =>5), ' 2 ' = = Array (' ID ' =>3, ' prize ' = ' speaker device ', ' V ' =>10), ' 3 ' = = Array (' ID ' =>4, ' Prize ' + ' 4G flash drive ', ' V ' =>12), ' 4 ' = = Array (' ID ' =>5, ' prize ' = ' 10Q ', ' V ' =>22), ' 5 ' = = Array (' ID ' =&gt ; 6, ' prize ' + ' will be able to be in the next (OH ', ' V ' =>50),); /* * Each front page request, PHP Loop Award set Array, * Get the prize ID from the Get_rand by the probability calculation function. * Save the winning prizes in the array $res[' yes ', * while the remaining non-winning information is saved in $res[' no ', * finally output JSON data to the front page. */foreAch ($prize _arr as $key + = $val) {$arr [$val [' id ']] = $val [' V '];} $rid = Get_rand ($arr); Get the prize ID $res by probability [' yes '] = $prize _arr[$rid -1][' Prize '); Medium Prize unset ($prize _arr[$rid-1]); The prize is excluded from the array and the remaining awards are shuffle ($prize _arr); Scrambled array order for ($i =0; $i
 
  
 


  • 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.