PHP实现红包算法,php发红包算法

供参考,有其它想法,可以留言。 

/*
    $total_money:红包总金额
    $total_people:总人数/总分数
    $min_money:每份红包的最小金额
    $max_money:每份红包的最大金额
    */
    function redpack($total_money, $total_people, $min_money, $max_money)
    {
        $ret = array();
        $new_ret = array();
        $total_real_money = $total_money - $total_people * $min_money;
        $ret[0] = 0;
        for ($i = 1; $i < $total_people; $i++) {
            $ret[$i] = get_rand($ret[$i - 1], $total_real_money, ($max_money - $min_money));
        }
        sort($ret);
        for ($j = 0; $j < count($ret); $j++) {
            if ($j == count($ret) - 1) {
                $new_ret[count($ret) - 1] = $total_real_money - $ret[count($ret) - 1] + $min_money;
            } else {
                $new_ret[] = $ret[$j + 1] - $ret[$j] + $min_money;
            }
        }
        shuffle($new_ret);
        return $new_ret;
    }


    function get_rand($start, $end, $max)
    {
        $tmp = rand($start, $end);
        $total_max = $start + $max;
        if ($tmp > ($total_max) || empty($tmp)) {
            return get_rand($start, $end, $max);
        } else {
            return $tmp;
        }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值