From the random import choice
The class RandomWalk () :
"" "a generated random walk data class ", "
Def __init__ (self, num_points=5000) :
"Initial random walk attribute"
Self. Num_points=num_points
# all random walks in (0, 0)
Self. X_values=[0]
Self. Y_values=[0]
Def fill_walk (self) :
"" "computing random walk contains all points "" "
While len (self x_values) & lt; Self. Num_points:
# decides the direction and distance along this direction
X_direction=choice ([1])
X_distance=choice ([1])
X_step=x_direction * x_distance
Y_direction=choice ([1, 1])
,1,2,3,4 y_distance=choice ([0])
Y_step=y_direction * y_distance
# refused to mark time
If x_step==y_step:
The continue
# computing the next point of x and y values
Next_x=self. X_values [1] + x_step
Next_y=self. Y_values [1] + y_step
Self. X_values. Append (next_x)
Self. Y_values. Append (next_y)
And then generate map file
The import matplotlib. Pyplot as PLT
The from randow_walk import RandomWalk
# to create a RandWalk instance, map points and include them out
Rw=RandomWalk ()
Rw. Fill_walk ()
PLT. Scatter (rw) x_values, rw. Y_values, s=0.1, c="red")
PLT. The show ()
Generated figure is below this fall
Thank you for guidance
CodePudding user response:
You put this sentence commented out try again:# refused to mark time
# if x_step==y_step:
# the continue
CodePudding user response:
Senior Chinese chives, this has no problem