php红包退回通知,php红包
標(biāo)簽:
/** ? 轉(zhuǎn)http://www.oschina.net/code/snippet_1392428_54532 ? ? 謝謝?php_fangting
* @param $total [你要發(fā)的紅包總額]
* @param int $num [發(fā)幾個(gè)]
* @return array[生成紅包金額]
*/
function getRedGift($total, $num = 10)
{
$min = 0.01;
$wamp = array();
$returnData = array();
for ($i = 1; $i < $num; ++$i) {
$safe_total = ($total - ($num - $i) * $min) / ($num - $i); //紅包金額的最大值
if ($safe_total < 0) break;
$money = @mt_rand($min * 100, $safe_total * 100) / 100;//隨機(jī)產(chǎn)生一個(gè)紅包金額
$total = $total - $money;//剩余紅包總額
$wamp[$i] = round($money, 2);//保留兩位有效數(shù)字
}
$wamp[$i] = round($total, 2);
$returnData[‘MoneySum‘] = $wamp;
$returnData[‘newTotal‘] = array_sum($wamp);
return $returnData;
}
//測(cè)試
$data = getRedGift(100, 10);
print_r($data);
//result:
/*
Array
(
[1] => 8.7
[2] => 10.09
[3] => 6.23
[4] => 6.87
[5] => 0.47
[6] => 3.12
[7] => 7.52
[8] => 12.21
[9] => 20.53
[10] => 24.26
)*/
public function rands($total=0, $num=3, $bonus_type=1)
{
$m = $total;
// $total=10;//紅包總額
//$num=8;// 分成8個(gè)紅包,支持8人隨機(jī)領(lǐng)取
$min=0.01;//每個(gè)人最少能收到0.01元
$res = array();
for ($i=1;$i
{
$safe_total = ($total-($num-$i)*$min)/($num-$i);//隨機(jī)安全上限
$money = mt_rand($min*100,$safe_total*100)/100;
$total = bcsub($total, $money, 2);
$res[] = $money;
}
$res[]=? $m - array_sum($res);
return $res;
}
public function hongbaoAction()
{
// 發(fā)3個(gè)拼手氣紅包,總金額是100元
for ($i = 0 ;$i< 10000; $i++)
{
$bonus_items? = $this->rands(100, rand(3, 60), 1);
// 查看生成的紅包
$arr = array();
$arr[‘typeid‘] = $bonus_items[0];
$arr[‘nums‘]= $bonus_items[1];
$arr[‘change_nums‘] = $bonus_items[2];
$arr[‘dateline‘] = array_sum($bonus_items);// 校驗(yàn)總金額是不是正確,看看微信有沒(méi)有坑我們的錢
$this->mainModel->test($arr);
unset($arr, $bonus_items);
}
}
標(biāo)簽:
總結(jié)
以上是生活随笔為你收集整理的php红包退回通知,php红包的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: STM32F1 W5500 TCP Cl
- 下一篇: centos7 访问php 该网页无法正