PHP winning probability algorithm, can be used for scraping cards, large turntable, such as lottery algorithm

Source: Internet
Author: User
Tags foreach array count end key

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

 --> $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, recording all the lottery award information, * where the ID of the winning rating, prize, said the prize, V to indicate the probability of winning.
 * Note that the V must be an integer, you can set the corresponding award of V to 0, which means that the probability of the award is 0, * the sum of V in the array (radix), the larger the cardinality can reflect the accuracy of the probability.
 * In this case the sum of V is 100, then the tablet computer corresponding to the probability of winning is 1%, * if the sum of V is 10000, the probability of winning is one out of 10,000. * */$prize _arr = Array (' 0 ' => array (' ID ' =>1, ' prize ' => ' tablet computer ', ' V ' =>1 '), ' 1 ' => array (' ID ' =>2, ' Prize ' => ' digital camera ', ' V ' =>5 ', ' 2 ' => array (' ID ' =>3, ' Prize ' => ' Speaker device ', ' V ' =>10), ' 3 ' => array (' ID ' =& Gt;4, ' Prize ' => ' 4G USB drive ', ' V ' =>12 ', ' 4 ' => array (' ID ' =>5, ' Prize ' => ' 10Q currency ', ' V ' =>22 '), ' 5 ' => Array ( 

' id ' =>6, ' Prize ' => ' next time may be in oh ', ' V ' =>50,);
 * * Each front-end page request, PHP cycle Awards set Array, * through the probability calculation function Get_rand get the award ID in the draw. * Save the winning prizes in the array $res[' yes '], * and the remaining outstanding information is saved in $res[' no '], * Finally output the JSON data to the front-end page. 
*/foreach ($prize _arr as $key => $val) {$arr [$val [' id ']] = $val [' V ']; } $rid = Get_rand ($arr); To obtain the award ID $res [' yes '] = $prize _arr[$rid -1][' Prize '); Award unset ($prize _arr[$rid-1]); Remove the award from the array, leaving the award shuffle ($prize _arr); Scramble 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.