先写了一个
random_walk.py文件,一个类用于生成散点图
- 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:
- #决定前进方向,沿这个方向前进
- #x
- x_direction = choice([1,-1])
- x_distance = choice([0,1,2,3,4])
- x_step = x_direction * x_distance