/*** Probability algorithm ** @ paramarray $ probability * @ returninteger | string */functionget_rand ($ probability) {// total probability precision of the probability array $ max = array_sum ($ probability );..."
/*** Probability algorithm ** @ param array $ probability * @ return integer | string */function get_rand ($ probability) {// total probability precision of the probability array $ max = array_sum ($ probability); foreach ($ probability as $ key => $ val) {$ rand_number = mt_rand (1, $ max); // random value from 1 to max if ($ rand_number <= $ val) {// if this value is less than or equal to the probability of the current award, we think that the winning return $ key;} else {$ max-= $ val; // otherwise, max minus the probability of the current award, then proceed to the calculation}/*** to sum up the analysis: * Make sure that the probability of each person obtaining the prize is the same * If a prize is missing, you should The probability of this lecture is changed to 0 * considering the high concurrency, after detecting that the user wins the prize, check whether the prize exists, if you don't have a prize, you can directly return the result * if you don't have a first-level prize *. then, you can return the result */} // Probability Ratio/*. Next, we will configure the award through PHP. */$ Data = array ("prize" => "tablet", "prob" => 1), array ("prize" => "digital camera ", "prob" => 3), array ("prize" => "speaker device", "prob" => 5), array ("prize" => "8g usb flash drive ", "prob" => 20), array ("prize" => "10Q coin", "prob" => 200 ), array ("prize" => "next time you may be able to win", "prob" => 771); foreach ($ data as $ key => $ val) {$ probability [$ key] = $ val ["prob"] ;}$ n = get_rand ($ probability ); $ res ['yes'] = $ data [$ n] ["prize"]; // $ res ['yes'] = $ data [$ n] [0]; unset ($ data [$ n]); // removes the Medium Award from the array, and the remaining non-Medium Award shuffle ($ data ); // disrupt the order of other awards $ func = create_function ('$ X', 'return $ x ["prize"];'); // $ res ['no'] = array_map (function ($ x) {return $ x [0] ;}, $ data ); // Other data except the winner $ res ['no'] = array_map ($ func, $ data); // Other data except the winner print_r ($ res );