【发布时间】:2018-07-22 16:13:53
【问题描述】:
我有以下 Python 代码。因为使用的是随机数,所以每次都会生成一个新的答案:
import random
import numpy as np
N = 64 # Given
T = 5 # Given
FinalLengths = []
for i in range(T):
c = range(1, N)
x = random.sample(c, 2) # Choose 2 random numbers between 1 and N-1
LrgstNode = max(x)
SmlstNode = min(x)
RopeLengths = [SmlstNode, LrgstNode - SmlstNode, N - LrgstNode]
S = max(RopeLengths)
N = S
FinalLengths.append(S)
avgS = np.mean(FinalLengths) # Find average
print("The mean of S is {}".format(avgS))
我的研究使我可能使用 itertools 组合来生成范围内的所有可能组合并让平均值收敛。如果有,怎么做?
谢谢。
【问题讨论】:
-
您想要所有尺寸 2 的组合,还是任何可能尺寸的所有组合?
标签: python loops for-loop random itertools