I am currently learning the random walk on Python, I came across the sample code that I couldn't explain please help me to clarify this
My question is that why does x no need to be put in the loop but the code is still able to operate. The other question is regarding the initial value of the loop step = random_walk[-1]
.
Thanks!
# 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)