[摘要]今天是最后一天上班,没什么事情,正好群里面的同学再讨论群红包的算法。特写了个算法。大致思路是找发过的红包,寻找它的随机震动坡度。下面来看看算法吧:#coding=gbkimport randomim...
今天是最后一天上班,没什么事情,正好群里面的同学再讨论群红包的算法。
特写了个算法。大致思路是找发过的红包,寻找它的随机震动坡度。
下面来看看算法吧:
#coding=gbk
import random
import sys
#print random.randint(0, 99)
#print "====", random.uniform(0, 0.99)
def calRandomValue(min, max, total, num):
print min, max, total, num
total = float(total)
num = int(num)
min = 0.01
if(num < 1):
return
if num == 1:
print "第%d个人拿到红包数为:%.2f" %(num, total)
return
i = 1
total_money = total
#rtotal = (total*100 - min*num*100)/100
while( i < num ):
max = total_money - min*(num- i)
k = int((num-i)/2)
if num -i <= 2:
k = num -i
max = max/k
monney = random.randint(int(min*100), int(max*100))
monney = float(monney)/100
total_money = total_money - monney
print "第%d个人拿到红包数为:%.2f, 余额为: %.2f" %(i, monney, total_money)
i += 1
print "第%d个人拿到红包数为:%.2f, 余额为: %.2f" %(i, total_money, 0.0)
if __name__ == "__main__":
min = sys.argv[1]
max = sys.argv[2]
total = sys.argv[3]
num = sys.argv[4]
calRandomValue(min, max, total, num)