# PHP winning probability algorithm and large turntable and other lottery algorithms

Source: Internet
Author: User

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 that records all the awards information for this lottery,

* The ID indicates the winning grade, prize the prize, v indicates the probability of winning.

* Note that 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 (cardinality), the larger the cardinality can reflect the accuracy of the probability.

* In this case the sum of V is 100, then the tablet computer's winning probability 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 ' =>4, ' Prize ' => ' 4G flash drive ', ' V ' =>12),

' 4 ' => array (' ID ' =>5, ' Prize ' => ' 10Q ', ' V ' =>22),

' 5 ' => array (' ID ' =>6, ' prize ' => ' will be able to be in the next time Oh ', ' V ' =>50),

);

/*

* Each front page request, PHP Loop Awards set Array,

* Through the probability calculation function Get_rand get the prize ID in the draw.

* Save the winning prizes in the array \$res[' yes '],

* and the rest of the outstanding information is kept 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); Get the award ID based on probability

\$res [' yes '] = \$prize _arr[\$rid -1][' Prize ']; Award in

unset (\$prize _arr[\$rid-1]); Remove the award from the array, leaving the award in the middle

Shuffle (\$prize _arr); Scrambling array Order

for (\$i =0; \$i

Related Keywords:

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.