目录
随机漫步简介
Python 示例
import random
"""
随机漫步生成是无规则的,是系统自行选择的结果.
根据设定的规则自定生成,上下左右的方位,每次所经过的方向路径.
"""
class Randomwalk():
"""
一个生成随机数漫步的类
"""
def __init__(self, num_point = 1000):
"""
初始化随机漫步的属性
"""
self.num_point = num_point
self.x_lab = [0]
self.y_lab = [0]
def fill_walk(self):
"""
计算随机漫步的所有点
"""
while len(self.x_lab) < self.num_point:
x_direction = random.choice([1, -1])
x_distance = random.choice([0, 1, 2, 3, 4])
x_step = x_direction * x_distance
y_direction = random.choice([1, -1])
y_distance = random.choice([0, 1, 2, 3, 4])
y_step = y_direction * y_distance
if x_step == 0 and y_step == 0:
continue
next_x = self.x_lab[-1] + x_step
next_y = self.y_lab[-1] + y_step
self.x_lab.append(next_x)
self.y_lab.append(next_y)
if __name__ == "__main__":
import matplotlib.pyplot as plt
rw = Randomwalk()
rw.fill_walk()
plt.scatter(rw.x_lab, rw.y_lab, s = 1)
plt.show()
while True:
rw = Randomwalk()
rw.fill_walk()
plt.figure(dpi = 128, figsize = (10, 6))
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.scatter(rw.x_lab, rw.y_lab, c = list(range(rw.num_point)), cmap = plt.cm.YlOrBr, edgecolors = None, s = 15)
plt.scatter(0, 0, c = 'green', edgecolors = None, s = 100)
plt.scatter(rw.x_lab[-1], rw.y_lab[-1], c = 'red', edgecolors = None, s = 100)
plt.show()
keep_running = input("Make another walk?(y/n): ")
keep_running = keep_running.lower()
if keep_running == 'n':
break