分类
PHP 编程语言

PHP红包算法类(已运用实际项目)

需求背景

    笔者在实际项目中需要用到抢红包这么一个功能,需求看起来很简单——写一个函数,根据金额和需要分发的红包个数结合随机数返回一个红包数组就成。

开始我也这么看,但到自己动手写这个所谓的函数时才发现原来并不是那么简单。

    最主要的问题就在于生成的红包总额会小于发放的总额。举个粟子:我发5元,10个红包,结果生成出来的数组金额之各却小于了10元。

    为了解决这个问题,参考了网上的一些代码,最后问题解决。不得不感慨一下——开源真伟大! 

    现在将代码分享出来给大家,也算是支持一下开源吧!

源码如下:

<?php
/**
 * User: 易学PHP [微信号:yixuephp]
 * Date: 2017/6/16
 * Time: 10:26
 */

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();

xiangzhanyou

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注