search
Homephp教程PHP源码微信红包算法

微信红包算法

May 22, 2016 pm 06:24 PM

微信红包算法

/**生成红包的函数*/
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;
    }
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools