Python微信红包算法

2015-06-16

红包分发随机额度差别比较大。

more red_package.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import random
import sys

def randBonus(min, max, total,num):
print min, max, total, num
#print "{:.2f}".format(3.1415029)
total = float(total)
num = int(num)
min = 0.01

if num < 1:
return
if num == 1:
print "the %d person:%.2f" % (num,total)
return
i = 1
totalMoney = total
while(i < num):
max = totalMoney - min*(num- i)
k = int((num-i)/2)
if num -i <= 2:
k = num -i
max = max/k
money = random.randint(int(min*100), int(max*100))
money = float(money)/100
totalMoney = totalMoney - money
#print "the %d person get:%.2f, Balance:%.2f"%(i,money,totalMoney)
print "the %d person get:%.2f"%(i,money)
i += 1

#print "the %d person get:%.2f, :%.2f"%(i,totalMoney,0.00)
print "the %d person get:%.2f哈哈"%(i,totalMoney)

if __name__ == '__main__':
if len(sys.argv) < 2 :
print "please usage %s num1 num2"%(sys.argv[0])
sys.exit()

min = 0.01
max = sys.argv[1]
total = sys.argv[1]
num = sys.argv[2]
randBonus(min, max, total, num)

例如:
./red_package.py 50 10
0.01 50 50 10
the 1 person get:4.96
the 2 person get:8.42
the 3 person get:11.46
the 4 person get:0.76
the 5 person get:10.59
the 6 person get:6.67
the 7 person get:5.49
the 8 person get:0.18
the 9 person get:1.45
the 10 person get:0.02

分类:Linux | 标签: |

相关日志

评论被关闭!