This article mainly introduces PHP programming to achieve the calculation of lottery probability algorithm, combined with a complete example of the PHP random number operation related operation skills, the need for friends can refer to the next
Specific as follows:
<?php//calculation of the probability function Get_rand ($PROARR) {$result = "; $proSum = Array_sum ($PROARR); foreach ($proArr as $key = =) $proCur) {$randNum = Mt_rand (1, $proSum); if ($randNum <= $proCur) {$result = $key; Break } else {$proSum-= $proCur; }} unset ($PROARR); return $result; } $prize _arr = Array (' 0 ' = = Array (' ID ' =>1, ' prize ' = ' home appliance ', ' V ' =>2), ' 1 ' = = Array (' ID ' =>2, ' prize ' = ') Digital camera ', ' V ' =>5), ' 2 ' = = Array (' ID ' =>3, ' prize ' = ' iPad ', ' V ' =>13), ' 3 ' = = Array (' ID ' =>4, ' prize ' = ' LED display ', ' V ' =>15, ' 4 ' = = Array (' ID ' =>5, ' prize ' = ' u disk ', ' V ' =>25), ' 5 ' = = Array (' ID ' =>6, ' prize ' = > ' keyboard ', ' V ' =>30), ' 6 ' = = Array (' ID ' =>7, ' prize ' = ' mouse pad ', ' V ' =>10),); foreach ($prize _arr as $key + $val) {$arr [$val [' id ']] = $val [' V '];} $rid = Get_rand ($arr); $res [' yes '] = $prize _arr[$rid -1][' Prize ']; unset ($prize _arr[$rid-1]); Shuffle ($prize _arr); $prize _arrcount = count ($prize _arr); for ($i =0; $i < $prize _arrcount; $i + +) {$PR [] = $prize_arr[$i [' Prize ']; } $res [' no '] = $PR; Lottery results $ro = $res [' Yes ']; Print_r ($ro);? >
Operation Result:
Ipad