登录  /  注册

分享实现PHP红包算法的思路(附开发代码)

藏色散人
发布: 2021-09-03 15:50:59
转载
3522人浏览过
根据很多需求的使用场景,如发红包、砍价类需求,这两个功能都有一个同样的特点,如下:
登录后复制

红包

1.总金额
2.红包个数
3.最小红包数量

砍价

1.砍价总金额
2.需要多少人完成砍价(人数根据需求而定)

  • 固定砍价人数
  • 随机砍价人数
  • 指定随机砍价人数
    第2点三个规则都需要根据规则得出一个人数

3.最小砍价金额

开发思路

验证参数

最小金额不允许小于0
总金额不允许大于数量乘最小金额

分配金额

取得平均金额(总金额/剩余数量)

分配金额  平均金额小于等于最金额时直接分配最小金额
获取金额幅度比例 最小值不允许小于 -1 最大值不允许大于 1
得出分配金额  幅度计算(平均值*(1+幅度比例))
分配金额判断 分配金额小于最小金额或者分配金额大于 可领取最大金额 ((最小金额+剩余总金额)-  (剩余数量×最小金额))时 重新分配金额
剩余最后一个则剩余所有金额都分配

开发代码

<?php /**
 * 发送红包
 * Class sandRed
 */
class sandRed
{
    #红包金额
    protected $amount;
    #红包个数
    protected $num;
    #领取的红包最小金额
    protected $minAmount;
    #红包分配结果
    protected $amountArr = [];

    public function __construct($amount, $num = 1, $minAmount = 1)
    {
        $this->amount = $amount;
        $this-&gt;num = $num;
        $this-&gt;minAmount = $minAmount;
    }

    /**
     * 处理返回
     * @return array
     * @throws Exception
     */
    public function handle()
    {
        # 验证
        if ($this-&gt;amount minAmount * $this-&gt;num) {
            throw new Exception('红包总金额必须≥'.$validAmount.'元');
        }
        # 分配红包
        $this-&gt;allot();
        return $this-&gt;amountArr;
    }

    /**
     * 分配红包
     */
    protected function allot()
    {
        # 剩余可分配的红包个数
        $num = $this-&gt;num;

        # 剩余可领取的红包金额
        $amount = $this-&gt;amount;
        while ($num &gt;= 1) {
            if ($num == 1) {
                # 剩余一个的时候,直接取剩余红包
                $coupon_amount = $this-&gt;formattingAmount($amount);
            } else {

                # 平均金额
                $avgAmount = $this-&gt;formattingAmount($amount / $num);

                # 分配金额
                $countAllotAmount = $this-&gt;countAllotAmount($avgAmount, $amount, $num);

                # 剩余的红包的平均金额
                $coupon_amount = $this-&gt;formattingAmount($countAllotAmount);
            }
            # 追加分配金额
            $this-&gt;amountArr[] = $coupon_amount;

            # 计算剩余金额
            $amount -= $coupon_amount;

            $num--;
        }
        # 随机打乱
        // shuffle($this-&gt;amountArr);
    }

    /**
     * 计算分配的红包金额
     * @param float $avgAmount 每次计算的平均金额
     * @param float $amount 剩余可领取金额
     * @param int $num 剩余可领取的红包个数
     * @return float
     */
    protected function countAllotAmount($avgAmount, $amount, $num)
    {
        # 如果平均金额小于等于最低金额,则直接返回最低金额
        if ($avgAmount minAmount) {
            return $this-&gt;minAmount;
        }
        # 浮动比率
        $floatingRate = $this-&gt;floatingRate();

        # 分配金额
        $allotAmount = $avgAmount * (1 + $floatingRate);

        # 浮动计算
        $coupon_amount = $this-&gt;formattingAmount($allotAmount);

        # 如果低于最低金额或超过可领取的最大金额,则重新获取
        if ($coupon_amount minAmount || $coupon_amount &gt; $this-&gt;canReceiveMaxAmount($amount, $num)) {
            return $this-&gt;countAllotAmount($avgAmount, $amount, $num);
        }
        return $coupon_amount;
    }

    /**
     * 计算分配的红包金额-可领取的最大金额
     * @param $amount
     * @param $num
     * @return float|int
     */
    protected function canReceiveMaxAmount($amount, $num)
    {
        return $this-&gt;minAmount + $amount - $num * $this-&gt;minAmount;
    }

    /**
     * 红包金额浮动比例
     * @return float|int
     */
    protected function floatingRate()
    {
        # 60%机率获取剩余平均值的大幅度红包(可能正数、可能负数)
        if (rand(1, 100) 
登录后复制
  • 总金额
$amount = 1;
登录后复制
  • 分配数量
$num = 10;
登录后复制
  • 最小金额
$minAmount = 0.01;

$red = new sandRed($amount, $num, $minAmount);

$res = $red-&gt;handle();
print_r($res);
登录后复制
  • 输出结果  [0.10,0.04,0.08,0.04,0.16,0.14,0.11,0.13,0.11,0.09]
echo array_sum($res);
登录后复制
  • 输出结果 1

推荐学习:《PHP视频教程

以上就是分享实现PHP红包算法的思路(附开发代码)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:segmentfault网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号