微信发红包是最近非常火的一件事情,大家或多或少的都发过或者收到过红包,那么下面来看一个我们在生活中常碰到的一个php实现微信红包的程序算法,希望这个程序
使用PHP发红包,当我们输入红包数量和总金额后,PHP会根据这两个值进行随机分配每个金额,保证每个人都能领取到一个红包,每个红包金额不等,就是要求红包金额要有差异,所有红包金额总额应该等于总金额。
我们先来分析下规律。
设定总金额为10元,有N个人随机领取:
N=1 第一个
则红包金额=X元;
N=2 第二个
为保证第二个红包可以正常发出,第一个红包金额=0.01至9.99之间的某个随机数。
第二个红包=10-第一个红包金额;
N=3 第三个
红包1=0.01至9.99之间的某个随机数
红包2=0.01至(10-红包1-0.01)的某个随机数
红包3=10-红包1-红包2
……
于是我们得到一个规律,在分配当前红包金额时,先预留剩余红白所需最少金额,然后在0.01至总金额-预留金额间取随机数,得到的随机数就是当前红包分配的金额。
实际应用中,程序先将红包金额分配好,即发红包时,红包个数以及每个红包的金额都分配好了,那么用户来抢红包时,我们随机给用户返回一个红包即可。
红包分配代码:
$total=20;//红包总金额 $num=10;// 分成10个红包,支持10人随机领取 $min=0.01;//每个人最少能收到0.01元 for ($i=1;$i
运行以上代码,会输出以下结果:
第1个红包,金额2.08元,余额17.92元
第2个红包,金额1.81元,余额16.11元
第3个红包,金额0.15元,余额15.96元
第4个红包,金额1.61元,余额14.35元
第5个红包,金额1.11元,余额13.24元
第6个红包,金额1.51元,余额11.73元
第7个红包,金额1.21元,余额10.52元
第8个红包,金额2.58元,余额7.94元
第9个红包,金额5.4元,余额2.54元
第10个红包,金额2.54元,余额0元
附上完整代码:
html代码
php代码
$i, 'money' => $money, 'total' => $total ); } $arr['res'][$num] = array('i'=>$num,'money'=>$total,'total'=>0); $arr['msg'] = 1; echo json_encode($arr); ?>
以上所述就是本文的全部内容了,希望对大家熟练应用php完成派红包程序有所帮助。
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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.

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.