用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

生成红包概率算法

2018-01-02 作者:blueboyzz举报

[php]代码库

/**生成红包的函数*/
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;
    }


分享到:
更多

网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。