本文為大家分享了Python3隨機(jī)漫步生成數(shù)據(jù)并繪制的具體代碼,供大家參考,具體內(nèi)容如下

random_walk.py
from random import choice
#生成隨機(jī)漫步的數(shù)據(jù)類
class RandomWalk():
def __init__(self,num_points=5000): #初始化隨機(jī)漫步的屬性
self.numpoints=num_points #隨機(jī)漫步的默認(rèn)點(diǎn)數(shù)
self.x_values=[0] #所有的隨機(jī)漫步都始于(0.0)
self.y_values=[0]
def fill_walk(self):
while len(self.x_values)<self.numpoints:
#決定前進(jìn)方向及前進(jìn)方向的距離
x_direction=choice([1,-1])
x_distance=choice([0,1,2,3,4])
x_step=x_direction*x_distance
y_direction=choice([1,-1])
y_distance=choice([0,1,2,3,4])
y_step=y_direction*y_distance
#拒絕原地踏步
if x_step==0 and y_step==0:
continue
#計(jì)算下一個點(diǎn)的x和y的值
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)
網(wǎng)頁題目:Python3隨機(jī)漫步生成數(shù)據(jù)并繪制-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://www.jbt999.com/article48/eehep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容