Recently the company has two activities, one is the egg throwing activity, the other is the turntable activity.
Backstage this need to do interface docking, when the user in the foreground click on the lottery, send AJAX requests to the background, the background for business processing, including recording the user's winning information, and then return the JSON format of the data to the foreground to display the most core of which is the winning algorithm logic.
After looking at some algorithms on the internet, it is found that the following algorithm is more efficient.
1<?PHP2 //Winning Prizes3 $prize _arr=Array(4 50=>Array(' id ' =>1, ' prize ' = ' cash 500W ', ' V ' =>1),//probability is 1/20061=>Array(' id ' =>2, ' prize ' = ' Iphone7 ', ' V ' =>5),72=>Array(' id ' =>3, ' prize ' = ' Nike running shoes ', ' V ' =>10),83=>Array(' id ' =>4, ' prize ' = ' magic sound headset ', ' V ' =>24),94=>Array(' id ' =>5, ' prize ' = ' Bluetooth sound ', ' V ' =>60),Ten5=>Array(' id ' =>6, ' prize ' = ' cash $1 ', ' V ' =>100) One A ); - - the /* - * Processing the array - */ - + foreach($prize _arr as $k=$v ){ - //using the new array item + $item[$v[' ID ']] =$v[' V ']; A } at - /* - Array ( - 1 = 1, - 2 = 5, - 3 = ten, in 4 =, - 5 =, to 6 = + ); - */ the * functionGet_rand ($item){ $ Panax Notoginseng $num=Array_sum($item);//calculates the denominator - the foreach($item as $k=$v ){ + A $rand=Mt_rand(1,$num);//probability interval (integer) includes 1 and the /* + * This algorithm is 666 - */ $ if($rand<=$v ){ $ //loop traversal, when subscript $k = 1, only $rand = 1 to win - $result=$k; - Echo $rand.‘ --‘.$v; the Break; -}Else{Wuyi //when the current standard $k=6, if $rand>100 must $rand < = 100 to win, then the $rand probability interval after the front 5 cycles = 200-1-5-10-24-60 (1,100) must be 1 dollars the $num-=$v; - Echo‘*‘.$rand.‘ *‘."   "."   "."   "; Wu } - } About $ return $result; - } - - $res= Get_rand ($item); A $prize=$prize _arr[$res-1][' Prize ']; + Echo $prize;
The output gets:
Loop traversal, constantly get random numbers and reduce the probability space, if the first prize only through a layer of circulation, the greater the probability, after the number of cycles more like high school probability of the topic, if not five prize, then must be six prize!
PHP Winning algorithm logic