For Python中的随机游走循环

问题描述

我目前正在学习使用Python进行随机游走的过程,遇到了无法解释的示例代码,请帮我澄清一下

我的问题是,为什么x不需要放在循环中,但是代码仍然可以运行。另一个问题是关于循环step = random_walk[-1]的初始值。

谢谢!

# Numpy is imported,seed is set

# Initialize random_walk

random_walk = [0]

# Complete the ___
for x in range(100) :   # the first question is at here.
    # Set step: last element in random_walk
    step = random_walk[-1] # the other question is at here.
 
    # Roll the dice
    dice = np.random.randint(1,7)

    # Determine next step
    if dice <= 2:
        step = step - 1
    elif dice <= 5:
        step = step + 1
    else:
        step = step + np.random.randint(1,7)

    # append next_step to random_walk
    random_walk.append(step)

# Print random_walk
print(random_walk)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)