search
Homephp教程PHP源码生成指定个数的随机红包

指定红包金额,最小红包金额和红包个数就可以自动生成

[PHP]代码   

<?php
$total=20;//红包总金额   
$num=10;// 分成10个红包,支持10人随机领取   
$min=0.01;//每个人最少能收到0.01元   
 $redpack = new redpack($total,$num,$min);
 $jieguo = $redpack->getPack();
 foreach($jieguo as $key=>$val){
	 $n = $key+1;
	 echo &#39;第&#39;.$n.&#39;个红包:&#39;.$val[&#39;money&#39;].&#39; 元,余额:&#39;.$val[&#39;balance&#39;].&#39; 元<br>&#39;;   
 }

class redpack{
	//总金额
	private $total=0;
	//红包数量
	private $num=0;
	//最小红包金额
	private $min=0.01;
	
	public function __construct($total,$num,$min)
	{
		$this->total = $total;
		$this->num = $num;
		$this->min = $min;
	}
	//红包结果
	public function getPack()
	{
		$total = $this->total;
		$num = $this->num;
		$min = $this->min;
		for ($i=1;$i<$num;$i++)   
		{   
			$safe_total=($total-($num-$i)*$min)/($num-$i);//随机安全上限   
			$money=mt_rand($min*100,$safe_total*100)/100;   
			$total=$total-$money;  
			//红包数据
			$readPack[]= [
				&#39;money&#39;=>$money,
				&#39;balance&#39;=>$total,
			];
		}
		//最后一个红包,不用随机		
		$readPack[] = [
			&#39;money&#39;=>$money,
			&#39;balance&#39;=>0,
		];
		//返回结果
		return $readPack;
	}
	
}

                   

                   

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 Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use