python3.7实现
import random
from functools import reduce, wraps
import time
def runtime(f):
@wraps(f)
def func(*args, **kwargs):
start_time = time.time()
res = f(*args, **kwargs)
print('运行时间:', (time.time() - start_time) * 1000)
return res
return func
def avg_bags(total=10, num=5):
tmp_num = round(total / num, 2)
arry = [tmp_num for i in range(num - 1)]
arry.insert(random.randint(0, num - 1), round(total - tmp_num * (num - 1), 2))
return arry
# 网上的例子 时间都差不多
@runtime
def redbags(money, num=10):
choice = random.sample(range(1, money * 100), num - 1)
choice.extend([0, money * 100])
choice.sort()
return [(choice[i + 1] - choice[i]) / 100 for i in range(num)]
@runtime
def random_bags(total=1, num=10):
arry = [1] * num
sum = num
for i in range(num - 1):
num_tm