随机发红包
# 写一个发红包,接受两个参数:金额 个数 import random money = 200 num = 10 ret = random.sample(range(1, money*100),num-1) ret.sort() ret.insert(0,0) ret.append(money*100) lst = [] for i in range(len(ret)-1): money = (ret[i+1]-ret[i])/100 lst.append(money) print(lst)
import random lst = [] def lucky_money(money,num): ret = random.sample(range(1, money*100), num-1) ret.sort() ret.insert(0, 0) ret.append(money*100) for i in range(len(ret)-1): money = (ret[i+1]-ret[i])/100 lst.append(money) return lst lucky_money(100,3) print(lst)
import random def lucky_money(money,num): ret = random.sample(range(1, money*100), num-1) ret.sort() ret.insert(0, 0) ret.append(money*100) for i in range(len(ret)-1): money = (ret[i+1]-ret[i]) yield money/100 for money in lucky_money(200,10): print(money)