python 发红包的小程序
红包1 import random def redpacket(cash,person): lst=[] sum1=0 cash = cash * 100 while cash>0 and person >1: money= random.randint(1,cash) person-=1 cash-=money lst.append(money/100) sum1 += money/100 else: lst.append(cash/100) sum1 += cash/100 random.shuffle(lst) return lst,sum1 print(redpacket(100,6))
红包2 import random def redpacket(cash,person): cash=cash*100 ret = random.sample(range(1,cash),person-1) ret.sort() ret.insert(0,0) ret.append(cash) for i in range(len(ret)-1): yield (ret[i+1]-ret[i])/100 res=redpacket(100,5) for money in res: print(money)
红包3
import random def redpacket(num,money): sum = 0 lst=[] for i in range(num): cash=random.randint(1,money) sum+=cash lst.append(cash) for i in range(len(lst)): x=round((lst[i]/sum)*100,2) yield x result = redpacket(5,100) for i in result: print(i)