方法一:
import random
def red_pack(money, num):
money = money*100
lst = random.sample(range(money), num-1)
lst.sort()
lst.insert(0, 0)
lst.append(money)
print(lst)
for i in range(len(lst)-1):
result = (lst[i+1] - lst[i])/100
yield result
ret = red_pack(200, 10)
for j in ret:
print(j)
=======================================================
方法二:概率
import random
def res_pack(money, num):
money = money * 100
lst1 = []
for i in range(num):
num_ran = random.randint(0, money)
lst1.append(num_ran)
lst2 = []
lst1_sum = sum(lst1)
for j in lst1:
lst2.append(j/lst1_sum)
for n in lst2:
print(round(money*n/100,2))
res_pack(200, 10)