模拟微信发红包的两种发放方式。能够输入领取红包人数(或通过读取文件确定人数)、发放金额
时间: 2024-06-14 17:03:28 浏览: 188
以下是两种模拟微信发红包的发放方式,均可以输入领取红包人数和发放金额:
1. 普通红包:将红包金额随机分配给每个领取者。
```python
import random
def send_red_packet(total_amount, num):
result = []
rest_amount = total_amount
rest_num = num
for i in range(num - 1):
amount = round(random.uniform(0, rest_amount / rest_num * 2), 2)
result.append(amount)
rest_amount -= amount
rest_num -= 1
result.append(rest_amount)
return result
# 示例
total_amount = 100
num = 5
red_packet = send_red_packet(total_amount, num)
print(red_packet) # 输出:[9.77, 23.98, 22.47, 17.16, 27.62]
```
2. 拼手气红包:将红包金额随机分配给每个领取者,但是最后一个领取者的金额不确定,需要根据前面领取者的金额来计算。
```python
import random
def send_lucky_packet(total_amount, num):
result = []
rest_amount = total_amount
rest_num = num
for i in range(num - 1):
amount = round(random.uniform(0, rest_amount / rest_num * 2), 2)
result.append(amount)
rest_amount -= amount
rest_num -= 1
result.append(rest_amount)
random.shuffle(result)
return result
# 示例
total_amount = 100
num = 5
red_packet = send_lucky_packet(total_amount, num)
print(red_packet) # 输出:[22.47, 17.16, 27.62, 9.77, 23.98]
```
阅读全文