-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_walk.py
More file actions
38 lines (27 loc) · 1.03 KB
/
random_walk.py
File metadata and controls
38 lines (27 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from random import choice
class RandomWalk:
"""一个生成随机游走数据的类"""
def __init__(self, num_points=5000):
"""初始化随机游走的属性"""
self.num_points = num_points
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
"""计算随机游走包含的所有点"""
# 不断游走,直到列表达指定的长度
while len(self.x_values) < self.num_points:
x_step = self._get_step()
y_step = self._get_step()
if x_step == 0 and y_step == 0:
continue
# 计算下一个点的坐标值
x = self.x_values[-1] + x_step
y = self.y_values[-1] + y_step
self.x_values.append(x)
self.y_values.append(y)
def _get_step(self):
# 决定前进的方向和距离
x_y_direction = choice([1, -1])
x_y_distance = choice([0, 1, 2, 3, 4, 5, 6, 7, 8])
x_y_step = x_y_direction * x_y_distance
return x_y_step