What a better lottery algorithm for almighty Gods.
Reply content:
What a better lottery algorithm for almighty Gods.
return False
This is what I wrote earlier. Online with the popular is two algorithms, the probability is too high. Had to return to the textbook, and directly put all the winning ball and jamming ball into the basket, randomly pumped.
and draw directly, do not need to put back, we just have to give the prize, if the prize is not, directly as a thank you for participating in the award.
1, ' name ' = ' apple ', ' chance ' =>1), array (' ID ' =>2, ' name ' = ' Apple phone ', ' chance ' =>2), arr AY (' id ' =>3, ' name ' = ' $1000 ' shopping card ', ' chance ' =>3), array (' ID ' =>4, ' name ' = ' 300 yuan tableware ', ' chance ' =>4), Array (' ID ' =>5, ' name ' = ' $100 ' mobile prepaid card ', ' chance ' =>5)); $prize _form[0]=array (' id ' =>0, ' name ' = ' = ' Thank you for participating! '); foreach ($prize as $key = + $value) {//formatted array $prize _form[$value [' id ']]= $value; if ($value [' chance ']>0) {$p [$value [' id ']]= $value [' Chance ']; }} $result = $this->get_rand ($p); echo $prize _form[$result [' name ']; }/* * Prize ID must not be 0, 0 default is thank you for participating * $PROARR =array (' 1 ' = ' 1 ', ' 2 ' =>2) **/private Function Get_rand ($PROARR) {//number of Prizes $prize _number=count ($PROARR); foreach ($proArr as $key = = $value) { for ($i =0; $i < $value; $i + +) {//Generate winning ball $seekGroup []= ' P '. $key; }} $chance _sum=array_sum ($PROARR); The number of interfering balls $disturb _number= $prize _number*100-$chance _sum; Generate Interference Ball $disturb =range (1, $disturb _number); Put the ball in the basket $basket =array_merge ($seekGroup, $disturb); Shuffle ($basket); Draw $rand =rand (0, $prize _number*100-1); if (Strstr ($basket [$rand], "P")) {$result =str_replace (' P ', ' ', $basket [$rand]); }else{$result = 0; } return $result; }}?>
This problem is better solved ha, assuming there are 100 prizes, the release of the object also has 100 people, the 100 prizes are numbered called award0-99, the distribution of the object of 100 people are numbered called person0-99, the code is implemented as follows: