算法爲自創,非微信官方算法(僅供參考);如測出有問題,請及時反饋。感謝
原文:http://flc.ren/2018/04/701.htmlphp
2018-04-07html
<?php /** * 紅包分配算法 * * @example * $coupon = new Coupon(200, 5); * $res = $coupon->handle(); * print_r($res); * * @author Flc <2018-04-06 20:09:53> * @see http://flc.ren | http://flc.io | https://github.com/flc1125 */ class Coupon { /** * 紅包金額 * * @var float */ protected $amount; /** * 紅包個數 * * @var int */ protected $num; /** * 領取的紅包最小金額 * * @var float */ protected $coupon_min; /** * 紅包分配結果 * * @var array */ 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(200, 5, 30); $res = $coupon->handle(); print_r($res);
運行結果:http://www.dooccn.com/php7/#i...git