PHP 生成微信红包代码简单
废话不多说了,一切解释尽在代码中,具体代码如下所示:
/** * @param $total [你要发的红包总额] * @param int $num [发几个] * @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;//随机产生一个红包金额 $total = $total - $money;//剩余红包总额 $wamp[$i] = round($money, 2);//保留两位有效数字 } $wamp[$i] = round($total, 2); $returnData['MoneySum'] = $wamp; $returnData['newTotal'] = array_sum($wamp); return $returnData; } //测试 $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 )*/
相关推荐
哈嘿Blog 2020-10-26
明月清风精进不止 2020-07-05
PythonMaker 2020-07-05
xirongxudlut 2020-06-28
kkpiece 2020-06-16
qscool 2020-06-12
CloudXli 2020-06-11
vs00ASPNET 2020-06-09
Dimples 2020-06-08
kuoying 2020-06-07
JJandYY 2020-05-31
Wyt00 2020-05-30
liuyh 2020-04-03
CloudXli 2020-05-11
世樹 2020-05-11
bizercsdn 2020-05-10
joyjoy0 2020-05-09