PHP winning probability algorithm and big turntable and other lottery algorithm _php tutorial

Source: Internet
Author: User
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 the prize information for this draw,

* 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 award to 0, which means that the odds of the prize being pumped are 0,

* The sum of V in the array (cardinality), the larger the cardinality, the more the probability of accuracy can be expressed.

* 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, ' prize ' = ' digital camera ', ' V ' =>5),

' 2 ' = = Array (' ID ' =>3, ' prize ' = ' speaker device ', ' V ' =>10),

' 3 ' = = Array (' ID ' =>4, ' prize ' = ' 4G ', ' V ' =>12),

' 4 ' = = Array (' ID ' =>5, ' prize ' = ' 10Q ', ' V ' =>22),

' 5 ' = = Array (' ID ' =>6, ' prize ' = ' = ' next time it will be able to be in 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 winning prizes in array $res[' yes ',

* While the remaining non-winning information is kept 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 based on probability

$res [' yes '] = $prize _arr[$rid -1][' Prize ']; Chinese awards

unset ($prize _arr[$rid-1]); The award is excluded from the array, leaving the award

Shuffle ($prize _arr); Scrambled Array Order

for ($i =0; $i

http://www.bkjia.com/PHPjc/814672.html www.bkjia.com true http://www.bkjia.com/PHPjc/814672.html techarticle 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 annotation explanation, can understand $proCur) {$randNum = Mt_rand (1, $proSum);

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