PHP Random Draw

Source: Internet
Author: User
Tags getv

Seems to be some unreasonable, trouble everybody help to correct me! Thank you ~

<?PHPHeader("Content-type:text/html;charset=utf-8"); functionGetv ($k,$arr){         for($j=$k;$j>=0;$j--){            if($j==0){                $tmp[' B ']=intval(0); $tmp[' E ']=intval($arr[$j[' V ']); return $tmp; }Else{                $tmp[' B ']=intval($arr[$j-1][' E ']); $tmp[' E ']=intval($arr[$j-1][' E ']+$arr[$j[' V ']); return $tmp; }        }    }            functionDorand () {//Prizes        $prize _arr=Array(            ' 0 ' + =Array(' id ' =>1, ' prize ' = ' tablet ', ' V ' =>3), ' 1 ' + =Array(' id ' =>2, ' prize ' = ' digital camera ', ' V ' =>5), ' 2 ' =Array(' id ' =>3, ' prize ' = ' speaker device ', ' V ' =>10), ' 3 ' + =Array(' id ' =>4, ' prize ' = ' 4G usb ', ' V ' =>12), ' 4 ' =Array(' id ' =>5, ' prize ' = ' Q $10 ', ' V ' =>20), ' 5 ' =Array(' id ' =>6, ' prize ' = ' = ' will be able to be in oh ', ' V ' =>50),        ); //Calculate probability range         for($i= 0;$i<Count($prize _arr);$i++){            $tmp= Getv ($i,$prize _arr); $prize _arr[$i[' B '] =$tmp[' B ']; $prize _arr[$i[' e '] =$tmp[' E ']; }                //Get probability        foreach($prize _arr  as $key=$val) {            $arr[$val[' ID ']] [' V '] =$val[' V ']; $arr[$val[' ID ']] [' B '] =$val[' B ']; $arr[$val[' ID ']] [' e '] =$val[' E ']; $arr[$val[' ID ']] [' prize '] =$val[' Prize ']; //get the sum of probabilities            $sum+=$arr[$val[' ID ']] [' V ']; }        ///Generate random number        $r=Mt_rand(1,$sum); //Var_dump ($arr); Traversing Data        foreach($arr  as $k=$v){            //exit; Judging the probability interval            if(($v[' B ']<=$r&&$v[' E ']>=$r)){                Echo"Random number:".$r. "\ T". "Probability".$v[' B ']. " --".$v[' E ']. "\ T". " Total probability: ".$sum. "\ n"; //return the winning ID                $id=$k; return $prize=$arr[$k[' Prize '];  Break; }        }    }         for($z= 1;$z<=3;$z++){        Echo"\ r \ n".$z." "The Winning prizes are:". Dorand (). " <p> "; }    ?>

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.