php实现微信拼手气红包

 1 <?php  
 2 $result = sendHB(100, 10);  
 3 var_export($result);  
 4 echo array_sum($result);  
 5   
 6 /** 
 7  * 拼手气红包实现 
 8  * 生成num个随机数,每个随机数占随机数总和的比例*money_total的值即为每个红包的钱额 
 9  * 考虑到精度问题,最后重置最大的那个红包的钱额为money_total-其他红包的总额 
10  * 浮点数比较大小,使用number_format,精确到2位小数 
11  * 
12  * @param double $money_total  总钱额, 每人最少0.01,精确到2位小数 
13  * @param int $num 发送给几个人 
14  * @return array num个元素的一维数组,值是随机钱额 
15  */  
16 function sendHB($money_total, $num) {  
17     if($money_total < $num*0.01) {  
18         exit('钱太少');  
19     }  
20   
21     $rand_arr = array();  
22     for($i=0; $i<$num; $i++) {  
23         $rand = rand(1, 100);  
24         $rand_arr[] = $rand;  
25     }  
26   
27     $rand_sum = array_sum($rand_arr);  
28     $rand_money_arr = array();  
29     $rand_money_arr = array_pad($rand_money_arr, $num, 0.01);  //保证每个红包至少0.01  
30   
31     foreach ($rand_arr as $key => $r) {  
32         $rand_money = number_format($money_total*$r/$rand_sum, 2);  
33   
34         if($rand_money <= 0.01 || number_format(array_sum($rand_money_arr), 2) >= number_format($money_total, 2)) {  
35             $rand_money_arr[$key] = 0.01;  
36         } else {  
37             $rand_money_arr[$key] = $rand_money;  
38         }  
39   
40     }  
41   
42     $max_index = $max_rand = 0;  
43     foreach ($rand_money_arr as $key => $rm) {  
44         if($rm > $max_rand) {  
45             $max_rand = $rm;  
46             $max_index = $key;  
47         }  
48     }  
49   
50     unset($rand_money_arr[$max_index]);  
51     //这里的array_sum($rand_money_arr)一定是小于$money_total的  
52     $rand_money_arr[$max_index] = number_format($money_total - array_sum($rand_money_arr), 2);  
53       
54     ksort($rand_money_arr);  
55     return $rand_money_arr;  
56 } 

 

转载于:https://www.cnblogs.com/ygcool/p/8664805.html

以下是Java实现微信手气红包算法的代码: ```java import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Random; public class RedPacketUtil { /** * 拆分红包 * * @param totalAmount 红包总金额(单位:元) * @param totalPeople 红包总人数 * @return 拆分后的红包金额列表 */ public static List<BigDecimal> splitRedPacket(BigDecimal totalAmount, int totalPeople) { List<BigDecimal> amountList = new ArrayList<>(); Random random = new Random(); BigDecimal leftAmount = totalAmount; int leftPeople = totalPeople; for (int i = 0; i < totalPeople - 1; i++) { // 随机生成一个红包金额,范围为:0.01元 ~ 剩余平均值的两倍 BigDecimal amount = BigDecimal.valueOf(random.nextDouble()) .multiply(leftAmount.divide(BigDecimal.valueOf(leftPeople), 2, BigDecimal.ROUND_HALF_UP)) .multiply(BigDecimal.valueOf(2)) .setScale(2, BigDecimal.ROUND_HALF_UP); amountList.add(amount); leftAmount = leftAmount.subtract(amount); leftPeople--; } amountList.add(leftAmount); return amountList; } /** * 计算红包总金额 * * @param amountList 红包金额列表 * @return 红包总金额(单位:元) */ public static BigDecimal getTotalAmount(List<BigDecimal> amountList) { BigDecimal totalAmount = BigDecimal.ZERO; for (BigDecimal amount : amountList) { totalAmount = totalAmount.add(amount); } return totalAmount; } public static void main(String[] args) { BigDecimal totalAmount = BigDecimal.valueOf(10); // 红包总金额为10元 int totalPeople = 5; // 拆分红包的总人数为5人 List<BigDecimal> amountList = splitRedPacket(totalAmount, totalPeople); System.out.println(amountList); } } ``` 使用示例: ```java BigDecimal totalAmount = BigDecimal.valueOf(10); // 红包总金额为10元 int totalPeople = 5; // 拆分红包的总人数为5人 List<BigDecimal> amountList = RedPacketUtil.splitRedPacket(totalAmount, totalPeople); // 拆分红包 BigDecimal total = RedPacketUtil.getTotalAmount(amountList); // 计算红包总金额 ``` 实现思路: 1. 先定义一个红包总金额和拆分红包的总人数; 2. 初始化一个空的红包金额列表和一个Random对象; 3. 循环拆分红包,每次生成一个随机金额,范围为:0.01元 ~ 剩余平均值的两倍; 4. 将生成的红包金额添加到列表中,并更新剩余金额和剩余人数; 5. 最后将剩余金额作为最后一个红包金额添加到列表中; 6. 计算红包总金额,返回红包金额列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值