/**将一个数分成指定份数
* @param $money_total
* @param $num
* @return array
*/
function cuttingNumber($money_total,$num){
//随机生产$num个随机数
$randArr = [];
for ($i = 0;$i<$num;$i++){
array_push($randArr,rand(100,200));
}
$randArrSum = array_sum($randArr);
$n = $randArrSum/100;
for ($i = 0;$i<count($randArr);$i++){
$randArr[$i] = bcdiv($randArr[$i],$n,2);
}
$randArrSum = array_sum($randArr);
if($randArrSum>100){
$m = $randArrSum-100;
//给最大一个值减去$m
rsort($randArr);
$randArr[0]-=$m;
}else{
$m = 100-$randArrSum;
//给最小一个值加上$m
sort($randArr);
$randArr[0]+=$m;
}
$result = [];
for($i=0;$i<$num;$i++){
array_push($result,bcmul(bcdiv($randArr[$i],100,2),$money_total,2));
}
$resultSum = array_sum($result);
if($resultSum>$money_total){
$m = $resultSum-$money_total;
//给最大一个值减去$m
rsort($result);
$result[0]-=$m;
}else{
$m = $money_total-$resultSum;
//给最小一个值加上$m
sort($result);
$result[0]+=$m;
}
shuffle($result);
return $result;
}
|