PHP Lottery algorithm

Source: Internet
Author: User
Tags shuffle
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:


  
   
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.