拼手气红包算法_二倍均值法

使用二倍均值法进行的拼手气红包算法

假设M为总金额,N为抢红包人数,那么根据二倍均值法,每次抢到的金额 = 随机区间 (0, M / N X 2)

这个公式可以确保每个人获取的金额的平均值是相等的,不会受到先后顺序不同的影响。

比如说,有一个金额为100块的红包,10人分,那么:

100/10X2 = 20, 所以第一个人的随机范围是(0,20 ),平均可以抢到10元。

假设第一个人随机到10元,那么剩余金额是100-10 = 90 元。

90/9X2 = 20, 所以第二个人的随机范围同样是(0,20 ),平均可以抢到10元。

假设第二个人随机到10元,那么剩余金额是90-10 = 80 元。

80/8X2 = 20, 所以第三个人的随机范围同样是(0,20 ),平均可以抢到10元。

以此类推,每一次随机范围的均值是相等的。

#!/usr/bin/python
#-*- coding utf-8 -*-

__author__ = 'Luo Chenyi'
__data__ = '2020-12-17'
__vertion__ = 'v1.00.00'

import random

MANUAL_SET = False
if MANUAL_SET == True:
    total_momey = input("Please input the amount of money:")
    total_num = input("How many people:")
    total_times = input("How many cycles:")
else:
    total_momey = 1000
    total_num = 10
    total_times = 1000

#输出每次的抢红包结果
DEBUG_INF_FOR_EACH_RED_PACKET_SNATCHING = False


#输入:
#   momey:总金额
#   num:总人数
#   times:总次数
#返回:
#   无
def red_packet(money, num, times=1):
    if money == 0:
        raise AttributeError('The amount should greater than 0.')
    elif num == 0:
        raise AttributeError('The num can not be zero.')

    #参数初始化和定义
    round = num                                 #剩余人数
    current_money = money * 100                 #当前剩余的钱,初始值为money,乘100倍表示精确到分
    avg = (current_money * times) / num         #平均值每人抢times的总金额 = 总金额 * times / 人数
    circle_times = 0                            #循环进行的次数
    variance = 0                                #方差,方差 = (每个人抢times次的总金额-平均值每人抢times的总金额)的平方和/总人数

    L = []  #定义一个空表,用来记录每个人抢到的红包金额
    for i in range(0, num):
        L.append(0)

    while  circle_times < times:
        circle_times += 1
        #赋值,准备抢红包
        round = num                     #剩余人数
        current_money = money * 100     #当前剩余的钱,初始值为money,乘100倍表示精确到分

        if DEBUG_INF_FOR_EACH_RED_PACKET_SNATCHING == True:
            #输出本次抢红包结果
            print('********************************************')
            print('Number of red packets snatched: %d' %circle_times)

        #计算前(num-1)个红包金额
        for i in range(1, num):
            get_money = random.randint(1, int((2 * current_money // round) - 1))
            L[i - 1] += get_money
            current_money -= get_money
            round -= 1
            if DEBUG_INF_FOR_EACH_RED_PACKET_SNATCHING == True:
                print('    id[%d] get %.02f rest %.02f' %(i, (float)(get_money / 100), (float)(current_money / 100)))

        #最后一个红包获得剩下的全部
        current_money, get_money = 0, current_money
        L[num - 1] += get_money
        if DEBUG_INF_FOR_EACH_RED_PACKET_SNATCHING == True:
            print('    id[%d] get %.02f rest %.02f' %(num, (float)(get_money / 100), (float)(current_money / 100)))
            print('********************************************')
            print('\n')

    #输出多次抢红包的最终结果
    if True:
        current_money = 0
        variance = 0
        print('********************************************')
        print(' Total money is %d, for %d people, circle %d times' %(money, num, times))
        print(' Average value is %.02f' %(float)(avg / 100))
        for i in range(1, num + 1):
            current_money += L[i - 1]
            variance += abs(L[i - 1] - avg) * abs(L[i - 1] - avg)
            print('    id[%d] totally get %.02f average each time %.02f' %(i, (float)(L[i - 1] / 100), (float)(L[i - 1] / 100 / times)))
        print(' The variance : %.2f' %(variance / num / 10000))
        print(' Amount received : %.2f' %(current_money / 100))
        print('********************************************')

if __name__ == '__main__':
    red_packet(total_momey, total_num, total_times)

下图是输出的结果,1000元的红包,10人抢,共发1000次,方差公式为
方差 = (每个人抢times次的总金额-平均值每人抢times的总金额)的平方和/总人数
方差有点吓人啊。。。。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
还是需要用线段切割法试试,二倍均值法是平均分配,与算法不符。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是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、付费专栏及课程。

余额充值