GithubHelp home page GithubHelp logo

redenvelop's Introduction

过年很多人会发微信的红包,但是为毛很多人说自己得不到最佳,因此作者写了一个微信红包发送的算法。

首先科普一下,微信红包的 规则 为:

红包金额的区间为 0.01 - 平均值的2倍

该规则为 微信团队公布的算法 ,读者可自行上网查找相关信息。

这也就是说,假设给10个人发送100元的红包,那么:

第一个人得到金额的区间为[0.01,20]

假设 前三个人 领到的红包为50元,那么此时红包还剩下 7个人 没有领取红包,红包还剩下 50元 ,那么下一个人可以得到的最大金额为:

(100-50)/(10-3)*2=14.29

第四个人得到的金额的区间为[0.01,14.29]

以此类推,最终可以将红包领完,python的代码为:

# 领红包的主要程序
def distribute(number, money):
    # 判断人数是不是整数
    if type(number) == type(1):
        moneymin = 0.01
        # 最大的数为平均数的2倍
        moneymax = money / number * 2
        # 判断钱是不是小于人头总数
        if money >= number * 0.01:
            getmoney = random.randint(1, 100) / 100 * moneymax
            if getmoney < moneymin:
                getmoney = moneymin
                return round(getmoney, 2)
            else:
                return round(getmoney, 2)
        else:
            print("钱少于人头数,请重新输入...")

    else:
        print("输入的不是整数,请重新输入...")


微信红包群的还有如下规则:

  1. 微信群 最多人数 默认500
  2. 微信红包 最大金额 为200元

因此,作者加入多进程后,程序运行的时间普遍在 0.5-0.6秒之间

说明该代码还是可行滴!

redenvelop's People

Contributors

ttyb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.