Php prize winning probability algorithm and big turntable lottery Algorithm

Source: Internet
Author: User

The php winning probability algorithm can be used to draw lottery algorithms such as scratch cards and big turntable. The usage is very simple. The Code has a detailed description, which can be understood at a glance.

$ ProCur ){

$ RandNum = mt_rand (1, $ proSum );

If ($ randNum <= $ proCur ){

$ Result = $ key;

Break;

} Else {

$ ProSum-= $ proCur;

}

}

Unset ($ proArr );

Return $ result;

}

/*

* Awards Array

* It is a two-dimensional array that records all the awards for this lottery,

* Id indicates the winning level, prize indicates the prize, and v indicates the winning probability.

* Note that the value of v must be an integer. You can set the value of v for the corresponding award to 0, which means that the percentage of the award is 0,

* The sum (base number) of values in the array. The larger the base number, the more accurate the probability is.

* In this example, if the sum of v is 100, the probability of winning a tablet is 1%,

* If the sum of v is 10000, the winning probability is one in ten.

*

*/

$ Prize_arr = array (

'0' => array ('id' => 1, 'prize' => 'tablet pc', '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 disks', 'V' => 12 ),

'4' => array ('id' => 5, 'prize' => '10q coin ', 'V' => 22 ),

'5' => array ('id' => 6, 'prize' => 'Next time you may be able to get in, ', 'V' => 50 ),

);

/*

* Each request to the front-end page sets an array of PHP loop awards,

* Use the probability calculation function get_rand to obtain the prize id.

* Save the prize to the array $ res ['yes,

* The remaining unwon information is saved in $ res ['no,

* Output the json data count to the front-end page.

*/

Foreach ($ prize_arr as $ key => $ val ){

$ Arr [$ val ['id'] = $ val ['V'];

}

$ Rid = get_rand ($ arr); // obtain the award id based on the probability.

$ Res ['yes'] = $ prize_arr [$ rid-1] ['prize']; // medium Award

Unset ($ prize_arr [$ rid-1]); // removes the medium Award from the array, leaving the remaining ones.

Shuffle ($ prize_arr); // disrupt the 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.