V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ropon
V2EX  ›  Python

微信发红包算法,求助

  •  
  •   ropon ·
    ropon · 2018-10-15 16:22:33 +08:00 · 2007 次点击
    这是一个创建于 1991 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如 100 元红包 10 个人抢,以下算法:先随机生成 10 个数,之和等于红包金额,然后依次从这个列表随机取一个
    ,取了后移除,在随机取一个,这样算法是否合理,公平不呢

    def hongbao(amount=5, count=3):
    s = []
    sum = 0
    min = 0.01
    max = float(amount) - count * 0.01
    num = 0
    for i in range(count-1):
    num = round(random.uniform(0, float(amount) - sum), 2)
    while num < min or num > max:
    num = round(random.uniform(0, float(amount) - sum), 2)
    sum += num
    s.append(num)
    s.append(round(float(amount) - sum, 2))
    # return s
    for i in range(count):
    temp = random.choice(s)
    print("第{0}个人抢的红包是{1}".format(i + 1, temp))
    s.remove(temp)


    hongbao(100, 10)

    测试有问题,问题在哪里呢
    第 1 条附言  ·  2018-10-15 16:56:30 +08:00
    def hongbao(amount=5, count=3):
    s = []
    sum = 0
    min = 0.01
    max = float(amount) - (count-1) * 0.01
    for i in range(count-1):
    num = round(random.uniform(0, float(amount) - sum), 2)
    while num < min or num > max:
    num = round(random.uniform(0, float(amount) - sum), 2)
    sum += num
    s.append(num)
    s.append(round(float(amount) - sum, 2))
    # return s
    for i in range(count):
    temp = random.choice(s)
    print("第{0}个人抢的红包是{1}".format(i + 1, temp))
    s.remove(temp)


    hongbao(100, 10)
    5 条回复    2018-10-15 22:26:51 +08:00
    beny2mor
        1
    beny2mor  
       2018-10-15 17:07:29 +08:00
    难读...
    crab
        2
    crab  
       2018-10-15 17:09:24 +08:00
    二倍均值法
    beny2mor
        4
    beny2mor  
       2018-10-15 17:12:49 +08:00
    我看了下那些 stackoverflow 的回复,
    最简单的是生成 10 个随机数,然后按照比例来分 100 元
    ropon
        5
    ropon  
    OP
       2018-10-15 22:26:51 +08:00
    整理思路如下:

    #算法一
    #################################################################
    def hongbao(amount=5, count=3):
    s = []
    min = 1 #分
    amount = amount * 100 #单位分
    max = amount - (count-1) #单位分
    for i in range(count-1):
    # num = random.randint(0, amount)
    num = random.uniform(0, int(amount))
    while num < min or num > max:
    num = random.uniform(0, int(amount))
    amount -= num
    s.append(round((num / 100), 2))
    s.append(round((amount / 100), 2))
    random.shuffle(s)
    return s
    print(hongbao(1, 6))
    ##############################################################

    算法二
    #############################################################
    # 0--|----|--------|-----------|-------20
    # 0 2 4 10 16 20

    def hongbao(amount=10, count=5):
    ret = random.sample(range(1, amount * 100), count - 1) #分为单位
    ret.extend([0, amount * 100]) #追加多个值 0 amount * 100
    ret.sort() #排序
    # return [((ret[i+1] - ret[i]) / 100 for i in range(num))] # 列表生产式
    for i in range(count):
    yield (ret[i+1] - ret[i]) / 100

    res = hongbao(10, 6)
    for i in res:
    print(i)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5276 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:18 · PVG 15:18 · LAX 00:18 · JFK 03:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.