夏夜,天上缀满了闪闪发光的星星,像细碎的流沙铺成的银河斜躺在青色的天宇上。大地已经沉睡了。我任了性,纵容思念开成一片海,定格成回忆里抹不去的风景。太阳把大海映红了,好像得大海披上了一层红纱。
网上发现了一个不错的php随机红包金额算法的类,测试了一下感觉很好用,记录下来。
php随机红包金额算法
php随机红包金额算法类
class Coupon
{
protected $amount;
protected $num;
protected $coupon_min;
protected $items = [];
/**
* 初始化
* @param float $amount 红包金额(单位:元)最多保留2位小数
* @param int $num 红包个数
* @param float $coupon_min 每个至少领取的红包金额
*/
public function __construct($amount, $num = 1, $coupon_min = 0.01)
{
$this->amount = $amount;
$this->num = $num;
$this->coupon_min = $coupon_min;
}
/**
* 处理返回
* @return array
*/
public function handle()
{
// A. 验证
if ($this->amount < $validAmount = $this->coupon_min * $this->num) {
throw new Exception('红包总金额必须≥'.$validAmount.'元');
}
// B. 分配红包
$this->apportion();
return [
'items' => $this->items,
];
}
/**
* 分配红包
*/
protected function apportion()
{
$num = $this->num;
$amount = $this->amount;
while ($num >= 1) {
// 剩余一个的时候,直接取剩余红包
if ($num == 1) {
$coupon_amount = $this->decimal_number($amount);
} else {
$avg_amount = $this->decimal_number($amount / $num); // 剩余的红包的平均金额
$coupon_amount = $this->decimal_number(
$this->calcCouponAmount($avg_amount, $amount, $num)
);
}
$this->items[] = $coupon_amount; // 追加分配
$amount -= $coupon_amount;
--$num;
}
shuffle($this->items); //随机打乱
}
/**
* 计算分配的红包金额
*
* @param float $avg_amount 每次计算的平均金额
* @param float $amount 剩余可领取金额
* @param int $num 剩余可领取的红包个数
* @return float
*/
protected function calcCouponAmount($avg_amount, $amount, $num)
{
// 如果平均金额小于等于最低金额,则直接返回最低金额
if ($avg_amount <= $this->coupon_min) {
return $this->coupon_min;
}
// 浮动计算
$coupon_amount = $this->decimal_number($avg_amount * (1 + $this->apportionRandRatio()));
// 如果低于最低金额或超过可领取的最大金额,则重新获取
if ($coupon_amount < $this->coupon_min
|| $coupon_amount > $this->calcCouponAmountMax($amount, $num)
) {
return $this->calcCouponAmount($avg_amount, $amount, $num);
}
return $coupon_amount;
}
/**
* 计算分配的红包金额-可领取的最大金额
* @param float $amount
* @param int $num
*/
protected function calcCouponAmountMax($amount, $num)
{
return $this->coupon_min + $amount - $num * $this->coupon_min;
}
/**
* 红包金额浮动比例
*/
protected function apportionRandRatio()
{
// 60%机率获取剩余平均值的大幅度红包(可能正数、可能负数)
if (rand(1, 100) <= 60) {
return rand(-70, 70) / 100; // 上下幅度70%
}
return rand(-30, 30) / 100; // 其他情况,上下浮动30%;
}
/**
* 格式化金额,保留2位
* @param float $amount
* @return float
*/
protected function decimal_number($amount)
{
return sprintf('%01.2f', round($amount, 2));
}
}使用方法
$coupon = new Coupon(10,8,0.01); $res = $coupon->handle(); print_r($res['items']);
打印结果:
Array ( [0] => 1.31 [1] => 1.14 [2] => 0.81 [3] => 0.96 [4] => 1.76 [5] => 1.31 [6] => 1.05 [7] => 1.05 [8] => 1.54 [9] => 0.51 [10] => 0.41 [11] => 0.44 [12] => 0.75 [13] => 0.71 [14] => 0.91 [15] => 0.69 [16] => 1.96 [17] => 0.85 [18] => 1.84 )
到此这篇关于php如何实现随机红包金额的方法就介绍到这了。月亮,她等待着太阳下班后,带着一块广阔无垠的深蓝色的幕布,来到了充满喜怒哀乐的人间。随即把深蓝色的幕布抛向天空,撒出许多黄色的亮闪闪的珍珠——星星。然后静静的、 安详地观看人间在她工作期间的生活。她很轻松,没有忧愁,没有困意,独自呆在那么大的天空。就这样,每当我睡不着坐在窗前时,月亮,便成了我的知心朋友。我就和她诉说着我的心里话。更多相关php如何实现随机红包金额的方法内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!