The php winning probability algorithm is a typical probability algorithm used in lottery algorithms such as scratch cards and big turntable. * The key is that this algorithm has been applied in our previous projects, especially for projects with large data volumes, the efficiency is very good.
$ 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, all prize information for this lottery is recorded. * The id indicates the prize level, the prize indicates the prize, and the v indicates the probability of the prize. * 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