<?php
/**
* User: phpmianshi.com
* Date: 2020/3/31
* Time: 10:16
*/
class redPack
{
/**
* 测试红包生成
*/
public function test(){
for($i=0;$i<5;$i++){
$num[$i]['a']=$this->getBonus(5,4);
}
var_dump($num);
}
/**
*生成红包数组
* @param $bonus_total 红包总额
* @param $bonus_count 红包个数
* @param $bonus_max 每个小红包的最大额
* @param $bonus_min 每个小红包的最小额
* @return 存放生成的每个小红包的值的一维数组
*/
function getBonus($total = 0, $count = 0)
{
$yushu = ($total - intval($total)); //如果金额为小数则取出小数位
$bonus_total = ($total - $yushu) * 100; //如果金额为小数则去除小数小计算分配
$bonus_count = $count;
$result = array();
if ($bonus_total / $bonus_count > 1) {
if (($bonus_total - $bonus_total / 4) / ($bonus_count - 1) >= 1) {
$bonus_max = $bonus_total / 4;
if (($bonus_total / 4) == ($bonus_total / $bonus_count)) {
$bonus_max += 50;
}
} else {
for ($j = 0; $j < $count; $j++) {
$result[$j] = ($bonus_total / $bonus_count) / 100;
}
$r = rand(0, $count - 1);
$result[$r] = ($bonus_total - $bonus_count * 1 + 1) / 100;
//如果还有负数产生就重新分配
$attr = array();
foreach ($result as $k => $v) {
$attr[$k]['money'] = $v;
$attr[$k]['yili'] = 0;
}
return $attr;
}
} else {
for ($k = 0; $k < $count; $k++) {
$result[$k] = $total / $count / 100;
}
//如果还有负数产生就重新分配
$attr = array();
foreach ($result as $k => $v) {
$attr[$k]['money'] = $v;
$attr[$k]['yili'] = 0;
}
return $attr;
}
$bonus_min = 1;
$average = $bonus_total / $bonus_count;
//$average = $bonus_total/ $bonus_count;
$a = $average - $bonus_min;
$b = $bonus_max - $bonus_min;
//这样的随机数的概率实际改变了,产生大数的可能性要比产生小数的概率要小。
//这样就实现了大部分红包的值在平均数附近。大红包和小红包比较少。
$range1 = $this->sqr($average - $bonus_min);
$range2 = $this->sqr($bonus_max - $average);
for ($i = 0; $i < $bonus_count; $i++) {
//因为小红包的数量通常是要比大红包的数量要多的,因为这里的概率要调换过来。
//当随机数>平均值,则产生小红包
//当随机数<平均值,则产生大红包
if (rand($bonus_min, $bonus_max) > $average) {
// 在平均线上减钱
$temp = $bonus_min + $this->xRandom($bonus_min, $average);
$result[$i] = $temp;
$bonus_total -= $temp;
} else {
// 在平均线上加钱
$temp = $bonus_max - $this->xRandom($average, $bonus_max);
$result[$i] = $temp;
$bonus_total -= $temp;
}
}
// 如果还有余钱,则尝试加到小红包里,如果加不进去,则尝试下一个。
while ($bonus_total > 0) {
for ($i = 0; $i < $bonus_count; $i++) {
if ($bonus_total > 0 && $result[$i] < $bonus_max) {
$result[$i]++;
$bonus_total--;
}
}
}
// 如果钱是负数了,还得从已生成的小红包中抽取回来
while ($bonus_total < 0) {
for ($i = 0; $i < $bonus_count; $i++) {
if ($bonus_total < 0 && $result[$i] > $bonus_min) {
$result[$i]--;
$bonus_total++;
}
}
}
//如果还有负数产生就重新分配
$attr = array();
//随机一个小红包加入金额小数位
$rands = rand(0, ($bonus_count - 1));
$result[$rands] += $yushu * 100;
$nums = 0;
//处理输出
foreach ($result as $k => $v) {
if ($v < 1) {
$this->getBonus();
die;
}
$attr[$k]['money'] = $v / 100;
$attr[$k]['yili'] = 0;
$nums += $v;
}
//dump($nums);
//dump($result);
return $attr;
}
/**
* 求一个数的平方
* @param $n
*/
function sqr($n)
{
return $n * $n;
}
/**
* 生成min和max之间的随机数,但是概率不是平均的,从min到max方向概率逐渐加大。
* 先平方,然后产生一个平方值范围内的随机数,再开方,这样就产生了一种“膨胀”再“收缩”的效果。
*/
function xRandom($bonus_min, $bonus_max)
{
$sqr = intval($this->sqr($bonus_max - $bonus_min));
$rand_num = rand(0, ($sqr - 1));
return intval(sqrt($rand_num));
}
}
//测试生成红包的数组
$redPack=new redPack();
$redPack->test();
————————————————
版权声明:本文为CSDN博主「私念」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tiancityycf/article/details/105216479
《本文》有 0 条评论