基于以上要求,有什么好的方式,可以實(shí)現(xiàn)呢
你看看這個(gè)行不行, 大概也就這樣思路.
/**
* 紅包分配算法
*
* 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;
/**
* 紅包個(gè)數(shù)
*
* @var int
*/
protected $num;
/**
* 領(lǐng)取的紅包最小金額
*
* @var float
*/
protected $coupon_min;
/**
* 紅包分配結(jié)果
*
* @var array
*/
protected $items = [];
/**
* 初始化
*
* @param float $amount 紅包金額(單位:元)最多保留2位小數(shù)
* @param int $num 紅包個(gè)數(shù)
* @param float $coupon_min 每個(gè)至少領(lǐng)取的紅包金額
*/
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. 驗(yàn)證
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; // 剩余可分配的紅包個(gè)數(shù)
$amount = $this->amount; //剩余可領(lǐng)取的紅包金額
while ($num >= 1) {
// 剩余一個(gè)的時(shí)候,直接取剩余紅包
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); //隨機(jī)打亂
}
/**
* 計(jì)算分配的紅包金額
*
* @param float $avg_amount 每次計(jì)算的平均金額
* @param float $amount 剩余可領(lǐng)取金額
* @param int $num 剩余可領(lǐng)取的紅包個(gè)數(shù)
*
* @return float
*/
protected function calcCouponAmount($avg_amount, $amount, $num)
{
// 如果平均金額小于等于最低金額,則直接返回最低金額
if ($avg_amount <= $this->coupon_min) {
return $this->coupon_min;
}
// 浮動(dòng)計(jì)算
$coupon_amount = $this->decimal_number($avg_amount * (1 + $this->apportionRandRatio()));
// 如果低于最低金額或超過(guò)可領(lǐng)取的最大金額,則重新獲取
if ($coupon_amount < $this->coupon_min
|| $coupon_amount > $this->calcCouponAmountMax($amount, $num)
) {
return $this->calcCouponAmount($avg_amount, $amount, $num);
}
return $coupon_amount;
}
/**
* 計(jì)算分配的紅包金額-可領(lǐng)取的最大金額
*
* @param float $amount
* @param int $num
*/
protected function calcCouponAmountMax($amount, $num)
{
return $this->coupon_min + $amount - $num * $this->coupon_min;
}
/**
* 紅包金額浮動(dòng)比例
*/
protected function apportionRandRatio()
{
// 60%機(jī)率獲取剩余平均值的大幅度紅包(可能正數(shù)、可能負(fù)數(shù))
if (rand(1, 100) <= 60) {
return rand(-70, 70) / 100; // 上下幅度70%
}
return rand(-30, 30) / 100; // 其他情況,上下浮動(dòng)30%;
}
/**
* 格式化金額,保留2位
*
* @param float $amount
*
* @return float
*/
protected function decimal_number($amount)
{
return sprintf('%01.2f', round($amount, 2));
}
}
此代碼轉(zhuǎn)載至PHPhuo.org用戶葉子坑, 侵刪!
PHP 實(shí)現(xiàn)微信紅包拆分算法
簡(jiǎn)單寫了一下,已發(fā)的紅包金額$count應(yīng)該保存到memcache或redis里,每次取發(fā)紅包將數(shù)值累加,不超過(guò)總金額。
示例:(已發(fā)的紅包金額臨時(shí)保存在靜態(tài)變量,需要改造)
function getRedPack($total, $min, $max){
static $count = 0;
$money = mt_rand($min, $max)/100;
$count += $money;
if ($count < $total) {
return $money;
}
return false;
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。