Php lottery probability algorithm (scratch card, big turntable), lottery scratch card

Source: Internet
Author: User

Php lottery probability algorithm (scratch card, big turntable), lottery scratch card

The example in this article shares with you the php winning probability algorithm, which can be used for scratch cards, big turntable, and other Lottery algorithms. Its usage is very simple. The Code provides detailed notes for your reference. The details are as follows:

<? Php/** classic probability algorithm. * $ proArr is a preset array. * assume that the array is array (100,200,300,400 ), * The start is to check whether the first number is within the probability range of 1,. * If not, the probability space is used, that is, the value of k minus the probability space of the number just now. * In this example, we subtract 100, that is, the second number is filtered within the range of 1,900. * In this way, there will always be a number that meet the requirements. * It is equivalent to touching something in a box. * The first is not, the second is not, and the third is not. The last one must be. * This algorithm is simple and highly efficient. * This algorithm is very efficient in projects with large data volumes. */Function get_rand ($ proArr) {$ result = ''; // The total probability precision of the probability array $ proSum = array_sum ($ proArr ); // probability array loop foreach ($ proArr as $ key => $ proCur) {$ randNum = mt_rand (1, $ proSum); if ($ randNum <= $ proCur) {$ result = $ key; break;} else {$ proSum-= $ proCur ;}} unset ($ proArr); return $ result ;} /** award array * is a two-dimensional array that records all prize information for this lottery. * The id indicates the winning level, the prize indicates the prize, and the 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, the sum of v is 100, and the probability of winning a tablet is 1%. * If the sum of v is 10000, the probability of winning a tablet is one in ten. **/$ 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 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 ),); /** for each request on the front-end page, PHP loop awards are set as an array. * The probability calculation function get_rand is used to obtain the prize id. * Save the prize to the array $ res ['yes'], and * Save the remaining unwon information 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, and the remaining unmiddle award shuffle ($ prize_arr ); // disrupt the array order for ($ I = 0; $ I <count ($ prize_arr); $ I ++) {$ pr [] = $ prize_arr [$ I] ['prize'];} $ res ['no'] = $ pr; print_r ($ res);?>

The above is all about the php lottery probability algorithm. I hope it will be helpful for you to learn php program design, and I hope you can also provide more support for the house of friends.

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.