Home > php教程 > php手册 > body text

微信红包和抽奖代码分享

WBOY
Release: 2016-06-07 11:34:57
Original
2172 people have browsed it

具体思路是,定义总钱数和人数,然后最小值为默认0.01元,然后进行循环,设置安全值是为了让后面的人都能拿到钱。仅自己尝试,有需求的可自己深入研究
微信红包代码:<?php <br /> 02<br>         // 微信红包算法<br> 03<br>          header("content-type:text/html;charset=utf-8");<br> 04<br>         $sum = 10;  //总价钱<br> 05<br>         $num = 8 ;  //人数<br> 06<br>         $min = 0.01;    //最少值<br> 07<br>         for($i=1;$i 08<br>             $row = ($sum-($num-$i)*$min)/($num-$i);// 安全值<br> 09<br>             $money = mt_rand($min*100,$row*100)/100;<br> 10<br>             $sum -= $money;<br> 11<br>             echo '第'.$i.'人,领取'.$money.'元,剩下'.$sum.'元<br>';<br> 12<br>         }<br> 13<br>         echo '第'.$num.'人,领取'.$sum.'元,剩下'.$sum.'元';<br> 14<br>     ?> 抽奖代码<?php <br /> 02<br>         // 抽奖<br> 03<br>      header("content-type:text/html;charset=utf-8");<br> 04<br>         function get_rand($arr){<br> 05<br>             $arr_sum = array_sum($arr);<br> 06<br>             $arr_rand = mt_rand(1,$arr_sum);<br> 07<br>             foreach($arr as $key => $arr_num){<br> 08<br>                 $arr_sum -= $arr_num;<br> 09<br>                 if($arr_rand>$arr_sum){<br> 10<br>                     return $key;<br> 11<br>                 }<br> 12<br>             }<br> 13<br>         }<br> 14<br>         $p = array(<br> 15<br>             '0' => array('id'=>1,'info'=>'一等奖','v'=>1),<br> 16<br>             '1' => array('id'=>2,'info'=>'二等奖','v'=>5),<br> 17<br>             '2' => array('id'=>3,'info'=>'三等奖','v'=>10),<br> 18<br>             '3' => array('id'=>4,'info'=>'四等奖','v'=>34)<br> 19<br>             );<br> 20<br>         foreach($p as $key => $value){<br> 21<br>             $arr[$value['id']] = $value['v'];<br> 22<br>         }<br> 23<br>         $rid = get_rand($arr);<br> 24<br>         $res['yes'] = $p[$rid-1]['info'];<br> 25<br>         unset ($p[$rid-1]) ;<br> 26<br>         shuffle ($p);<br> 27<br>         for($i=0;$i<count></count> 28<br>             $pr[]= $p[$i]['info'];<br> 29<br>         }<br> 30<br>         $res['no'] = $pr;<br> 31<br>         var_dump($res);<br> 32<br>     ?>

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template