创建随机类 RandomWalk:
# 随机漫步:每次行走完全随机,没有明确的方向,结果是由一系列随机决策决定的
# RandomWork 类,随机选择前进方向.
# 这个类需要三个属性,一个是存储随机漫步次数的变量,其他两个是列表,分别存储随机漫步经过的每个点的 x 坐标和 y 坐标
from random import choice
class RandomWalk:
# 生成随机漫步数的类
def __init__(self,num_points=5000):
self.num_points = num_points
# 所以随机漫步数都始于 (0,0)
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
# 计算随机漫步包含的所有的点
# 不断漫步,直到列表达到指定长度
while len(self.x_values) < self.num_points:
# 决定前进方向以及沿这个方向方向前进的距离
# choice([1, -1]) 给x_direction 选择一个值,结果要么表示向右走的1,要么表示向左走的-1.
# choice([0, 1, 2, 3, 4])随机选择一个 0~4 的整数,表示沿指定的方向走多远。
# 通过包含0,不仅能够同时沿两个轴移动,还能够只沿一个轴移动
x_direction = choice([1, -1])
x_distance = choice([0, 1, 2, 3, 4])
#