这篇文章主要为大家详细介绍了php生成红包概率算法简单示例,具有一定的参考价值,可以用来参考一下。
对php生成红包概率算法简单示例感兴趣的小伙伴,下面一起跟随四海网的小编巴闭妹来看看吧!
/**
*
* @param
* @arrange (512.笔记) www.q1010.com
**/
/**生成红包的函数*/
function getRandMoney($totalMoney, $totalPeople=2, $miniMoney=1){
$randRemainMoney = $totalMoney - $totalPeople * $miniMoney;//剩余需要随机的钱数
return _getRandMoney($randRemainMoney, $totalPeople, $miniMoney);
}
/**红包生成的逻辑代码*/
function _getRandMoney($totalMoney, $totalPeople, $miniMoney){
$returnMessage = array('status'=>1, 'data'=>NULL);
if($totalMoney > 0){
$returnMessage['data'] = _randMoney($totalMoney, $totalPeople, $miniMoney);
}elseif($totalMoney == 0){
$returnMessage['data'] = array_fill(0, $totalPeople, 1);
}else{
$returnMessage['status'] = -1;
$returnMessage['data'] = '参数传递有误,生成红包失败';
}
return $returnMessage;
}
/*参数无误,开始生成对应的红包金额*/
function _randMoney($totalMoney, $totalPeople, $miniMoney){
$data = array_fill(0, $totalPeople, $miniMoney);
if($totalPeople > 1){
foreach($data as $k => $v){
if($k == $totalPeople -1){
$data[$k] = $totalMoney + $v;
break;
}else {
if($totalMoney == 0) break;
$randMoney = rand(0, $totalMoney);
$totalMoney -= $randMoney;
$data[$k] = $randMoney + $v;
}
}
}
return $data;
}
本文来自:http://www.q1010.com/173/10968-0.html
注:关于php生成红包概率算法简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。