码迷,mamicode.com
首页 > 微信 > 详细

php微信红包算法

时间:2018-12-31 20:22:39      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:lse   get   mini   code   reac   失败   ==   amp   for   


微信红包算法.php


/**生成红包的函数*/
function getRandMoney($totalMoney, $totalPeople=2, $miniMoney=1){
 
        $randRemainMoney = $totalMoney - $totalPeople * $miniMoney;//剩余需要随机的钱数
        return _getRandMoney($randRemainMoney, $totalPeople, $miniMoney);
    }
 
/**红包生成的逻辑代码*/
function _getRandMoney($totalMoney, $totalPeople, $miniMoney){
 
        $returnMessage = array('status'=>1, 'data'=>NULL);
        if($totalMoney > 0){
            $returnMessage['data'] = _randMoney($totalMoney, $totalPeople, $miniMoney);
        }elseif($totalMoney == 0){
            $returnMessage['data'] = array_fill(0, $totalPeople, 1);
        }else{
            $returnMessage['status'] = -1;
            $returnMessage['data'] = '参数传递有误,生成红包失败';
        }
 
        return $returnMessage;
    }
 
/*参数无误,开始生成对应的红包金额*/
function _randMoney($totalMoney, $totalPeople, $miniMoney){
 
        $data = array_fill(0, $totalPeople, $miniMoney);
        if($totalPeople > 1){
            foreach($data as $k => $v){
                if($k == $totalPeople -1){
                    $data[$k] = $totalMoney + $v;
                    break;
                }else {
                    if($totalMoney == 0) break;
                    $randMoney = rand(0, $totalMoney);
                    $totalMoney -= $randMoney;
                    $data[$k] = $randMoney + $v;
                }                
            }
        }
        return $data;
    }

php微信红包算法

标签:lse   get   mini   code   reac   失败   ==   amp   for   

原文地址:https://www.cnblogs.com/lalalagq/p/10203131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!