ランダムウォーク

from random import choice 

#choice()を使用して、使用する選択肢を決定します-ランダムウォークの方法を決定し
ます

#xy 2つのリストは、ランダムウォークがクラスRandomWalk()を通過する各ポイントのxy座標を格納します
    "" "ランダムウォークデータタイプ "" " 

    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):
        "" "ランダムウォークに含まれるすべてのポイントを計算する" "" #len(self)の

        、リストの指定された長さに達するまで歩き続けます.x_values)<self.num_points:

            #移動方向とこの方向に
            移動する距離を決定しますx_direction = choice([1、-1])#1は右に移動することを意味し、-1は左に移動することを意味します
            x_distance = choice([0,1,2,3,4])#0〜4の整数をランダムに選択します。これは、距離を意味します。行く
            x_step = x_direction * x_distance#移動する距離を決定します

            y_direction = choice([1、-1])
            y_distance = choice([0,1,2,3,4])
            y_step = y_direction * x_distance 

            x_step == 0およびy_step == 0の場合、所定の位置へのステップを拒否
                続行

            #次のポイントの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 )

おすすめ

転載: blog.csdn.net/wyzworld/article/details/88244848