PHP红包分配算法

42 篇文章 0 订阅

 参考: http://www.phpmianshi.com/?id=3

<?php
/**
 * User: phpmianshi.com 
 * Date: 2020/3/31
 * Time: 10:16
 */
 
class redPack
{
    /**
     * 测试红包生成
     */
     public function test(){
        for($i=0;$i<5;$i++){
            $num[$i]['a']=$this->getBonus(5,4);
        }
        var_dump($num);
    }
 
    /**
     *生成红包数组
     * @param $bonus_total 红包总额
     * @param $bonus_count 红包个数
     * @param $bonus_max 每个小红包的最大额
     * @param $bonus_min 每个小红包的最小额
     * @return 存放生成的每个小红包的值的一维数组
     */
    function getBonus($total = 0, $count = 0)
    {
        $yushu = ($total - intval($total)); //如果金额为小数则取出小数位
        $bonus_total = ($total - $yushu) * 100; //如果金额为小数则去除小数小计算分配
        $bonus_count = $count;
        $result = array();
        if ($bonus_total / $bonus_count > 1) {
            if (($bonus_total - $bonus_total / 4) / ($bonus_count - 1) >= 1) {
                $bonus_max = $bonus_total / 4;
                if (($bonus_total / 4) == ($bonus_total / $bonus_count)) {
                    $bonus_max += 50;
                }
            } else {
                for ($j = 0; $j < $count; $j++) {
                    $result[$j] = ($bonus_total / $bonus_count) / 100;
                }
                $r = rand(0, $count - 1);
                $result[$r] = ($bonus_total - $bonus_count * 1 + 1) / 100;
                //如果还有负数产生就重新分配
                $attr = array();
                foreach ($result as $k => $v) {
                    $attr[$k]['money'] = $v;
                    $attr[$k]['yili'] = 0;
                }
                return $attr;
            }
        } else {
            for ($k = 0; $k < $count; $k++) {
                $result[$k] = $total / $count / 100;
            }
            //如果还有负数产生就重新分配
            $attr = array();
            foreach ($result as $k => $v) {
                $attr[$k]['money'] = $v;
                $attr[$k]['yili'] = 0;
            }
            return $attr;
        }
        $bonus_min = 1;
 
 
        $average = $bonus_total / $bonus_count;
        //$average = $bonus_total/ $bonus_count;
 
        $a = $average - $bonus_min;
        $b = $bonus_max - $bonus_min;
 
        //这样的随机数的概率实际改变了,产生大数的可能性要比产生小数的概率要小。
        //这样就实现了大部分红包的值在平均数附近。大红包和小红包比较少。
        $range1 = $this->sqr($average - $bonus_min);
        $range2 = $this->sqr($bonus_max - $average);
 
        for ($i = 0; $i < $bonus_count; $i++) {
            //因为小红包的数量通常是要比大红包的数量要多的,因为这里的概率要调换过来。
            //当随机数>平均值,则产生小红包
            //当随机数<平均值,则产生大红包
            if (rand($bonus_min, $bonus_max) > $average) {
                // 在平均线上减钱
                $temp = $bonus_min + $this->xRandom($bonus_min, $average);
                $result[$i] = $temp;
                $bonus_total -= $temp;
            } else {
                // 在平均线上加钱
                $temp = $bonus_max - $this->xRandom($average, $bonus_max);
                $result[$i] = $temp;
                $bonus_total -= $temp;
            }
        }
 
        // 如果还有余钱,则尝试加到小红包里,如果加不进去,则尝试下一个。
        while ($bonus_total > 0) {
            for ($i = 0; $i < $bonus_count; $i++) {
                if ($bonus_total > 0 && $result[$i] < $bonus_max) {
                    $result[$i]++;
                    $bonus_total--;
                }
            }
        }
 
        // 如果钱是负数了,还得从已生成的小红包中抽取回来
        while ($bonus_total < 0) {
            for ($i = 0; $i < $bonus_count; $i++) {
                if ($bonus_total < 0 && $result[$i] > $bonus_min) {
                    $result[$i]--;
                    $bonus_total++;
                }
            }
        }
 
        //如果还有负数产生就重新分配
        $attr = array();
 
        //随机一个小红包加入金额小数位
        $rands = rand(0, ($bonus_count - 1));
        $result[$rands] += $yushu * 100;
 
        $nums = 0;
        //处理输出
        foreach ($result as $k => $v) {
            if ($v < 1) {
                $this->getBonus();
                die;
            }
            $attr[$k]['money'] = $v / 100;
            $attr[$k]['yili'] = 0;
            $nums += $v;
        }
        //dump($nums);
        //dump($result);
        return $attr;
    }
 
    /**
     * 求一个数的平方
     * @param $n
     */
    function sqr($n)
    {
        return $n * $n;
    }
 
    /**
     * 生成min和max之间的随机数,但是概率不是平均的,从min到max方向概率逐渐加大。
     * 先平方,然后产生一个平方值范围内的随机数,再开方,这样就产生了一种“膨胀”再“收缩”的效果。
     */
    function xRandom($bonus_min, $bonus_max)
    {
        $sqr = intval($this->sqr($bonus_max - $bonus_min));
        $rand_num = rand(0, ($sqr - 1));
        return intval(sqrt($rand_num));
    }
}
 
//测试生成红包的数组
$redPack=new redPack();
$redPack->test();

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值