//php probabilistic lottery algorithm1, get the total number of probabilities2, random from 1 to total probability number3, determine whether the obtained random number is less than or equal (that is, if your random number is in the array is worth the range of array (1,2,3,4,5,6) is random out of a number of 3 3 is present in the array so the key is assigned to a variable, if a random number is 7, then subtract this range Until you can find it in the array.$sum=$sum-$valLike this (the random number is 7 7-1=6) ) The values in the arrayfunctionGet_rand ($arr){ $result=""; $sum=Array_sum($arr);//get the total probability book foreach($arr as $key=$val) { $rand=Mt_rand(1,$sum); //var_dump ($val [' xx ']); rad = 7; 7- if($rand<=$val)//gets whether the value is in Val { //the random number is less than or equal to the value $result=$key; Break; }Else{ $sum=$sum-$val; } } return $result; }
Oreach ($prize _arr as $key=$val) { $arr[$val[' ID ']] =$val[' V ']; } $rid= Getrand ($arr);//get the prize ID based on probability $res=$prize _arr[$rid-1];//Chinese Awards$min=$res[' min ']; $max=$res[' Max ']; if($res[' ID ']==7) {//Seven-Equal prize $i=Mt_rand(0,5); $result[' angle '] =Mt_rand($min[$i],$max[$i]); }Else{ $result[' angle '] =Mt_rand($min,$max);//randomly generates an angle} $result[' prize '] =$res[' Prize ']; EchoJson_encode ($result);
$prize _arr=Array( ' 0 ' + =Array(' id ' =>1, ' prize ' = ' tablet ', ' V ' =>1), ' 1 ' + =Array(' id ' =>2, ' prize ' = ' digital camera ', ' V ' =>3), ' 2 ' =Array(' id ' =>3, ' prize ' = ' speaker device ', ' V ' =>6), ' 3 ' + =Array(' id ' =>4, ' prize ' = ' 4G usb ', ' V ' =>20), ' 4 ' =Array(' id ' =>5, ' prize ' = ' 10Q ', ' V ' =>25), ' 5 ' =Array(' id ' =>6, ' prize ' = ' = ' will be able to be in oh ', ' V ' =>50), );/** Each front page request, PHP Loop Award set Array, * Get the prize ID from the Get_rand by the probability calculation function. * Save the winning prizes in the array $res[' yes ', * while the remaining non-winning information is saved in $res[' no ', * finally output JSON data to the front page. */foreach($prize _arr as $key=$val) { $arr[$val[' ID ']] =$val[' V '];//bring into array} $rid= Get_rand ($arr);//get the prize ID based on probability$res[' yes '] =$prize _arr[$rid-1][' Prize '];//Chinese Awardsunset($prize _arr[$rid-1]);//The award is excluded from the array, leaving the awardShuffle($prize _arr);//Scrambled Array Order for($i= 0;$i<Count($prize _arr);$i++){ $PR[] =$prize _arr[$i[' Prize ']; } $res[' no '] =$PR; Print_r($res);
PHP Lottery Program