import random
weights_name = ["Multi", "Equity 1", "Equity 2", "Equity 3", "FI", "Cash"]
weights = [0] * 6
weights[1] = 0.15 # Equity 1 weight
remaining = 0.85 # the sum of the remaining weights
x = random.randint(0, 100)
weights[-1] = x/1000 # Cash weigth
remaining -= weights[-1]
所以对于剩余的重量,你必须生成随机值,这样最后一个不会大于0.2
last_weight = remaining
while last_weight > 0.2:
last_weight = remaining
for i in [0, 2, 3]:
weights[i] = 0.05 + random.randint(0, 150)/1000 # generating a random no between 0.05 and 0.20
last_weight -= weights[i]
weights[4] = last_weight
for w in weights:
print(w)
你可以一个接一个地生成数字,在每一步计算你已经得到的权重的总和。 我将使用random.randint函数进行演示
所以对于剩余的重量,你必须生成随机值,这样最后一个不会大于0.2
我的输出:
相关问题 更多 >
编程相关推荐