Just started with python, so bear with me for some silly questions. Trying to figure out if the below code actually simulates a 1D random walk motion. The +/- 1 of respective random steps do not seem to add up in results.
Results:
positions: [0, 1, 2, 3, 4, 3]
rr = [0.38965102 0.88157087 0.60033975 0.84260495 0.44094328] # values to determine if positions should get +/-1
with this code:
import numpy as np
import matplotlib.pyplot as plt
steps = 10
positions = [2]
for i in range(steps):
rr = np.random.random(steps)
if rr[i] > 0.5:
positions.append(positions[i] + 1)
elif rr[i] < 0.5:
positions.append(positions[i] - 1
print(positions)
print(rr)
plt.plot(positions)
plt.show()