我正在尝试使用 LCG 方法制作随机生成的图。虽然情节行为有效,但我的问题是当我尝试将列表返回到变量时。我的目标是让递归函数将一个列表返回给一个变量,但它似乎在退出定义时什么都不做。
递归函数(内部定义):
def LCG_recursive(vmin, vmax):
for x in range(0, 1):
randnum = 19680801 # Fixing random state for reproducibility
randnum2 = [] # clearing list for next random set
break
for x in range(1, i+1):
randnum = ((a*randnum+b) % M) / M
randres = (vmax - vmin)*randnum + vmin #limit to range of plot
randnum2.append(randres)
if x>i:
return randnum2
来自:
# For each set of style and range settings, plot i random points in the box
# defined [23, 32], y in [0, 100], z in [zlow, zhigh].
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
xs = LCG_recursive(23, 32)
ys = LCG_recursive(0, 100)
zs = LCG_recursive(zlow, zhigh)
ax.scatter(xs, ys, zs, marker=m)
我的尝试是将种子设置为randnum并在每次检索随机生成的值列表时清除randnum2。但是,尝试单步执行该文件表明它成功执行了递归,但是当它移动到下一个轴列表时返回一个 NoneType 对象。
我尝试创建全局变量并检查执行递归操作后是否可以移动randnum2,但我没有任何运气。