Random Walk

4223 days ago by comphy

1 & 2 Dimensional Cases

BBakar - 26/05/12

1 Dimensional Case

import numpy as np import random # 3 walkers are considered step1 = 0 step2 = 0 step3 = 0 L1=[] # first walker L2=[] # second walker L3=[] # third walker for i in range(100000): step1 = step1 + 2*np.random.random_integers(0,1) - 1 L1.append(step1) step2 = step2 + 2*np.random.random_integers(0,1) - 1 L2.append(step2) step3 = step3 + 2*np.random.random_integers(0,1) - 1 L3.append(step3) W1 = list_plot(L1, color = "red", plotjoined=True) W2 = list_plot(L2, color = "black", plotjoined=True) W3 = list_plot(L3, color = "blue", plotjoined=True) W1 + W2 + W3 
       

                                
                            

                                

2 Dimensional Case

# Functions determining the walkers where to go (right-left or up-down) # 2 independent walkers are considered def right_left(step_rl): return (step_rl-1)*(step_rl+1)*(3-step_rl)/3 def up_down(step_ud): return step_ud*(step_ud-2)*(step_ud-4)/3 # ************************************************* P1 = [] P2 = [] step_x1 = 0 step_y1 = 0 step_x2 = 0 step_y2 = 0 for i in range(40000): rand_xy1 = np.random.random_integers(0,3) step_x1 = step_x1 + right_left(rand_xy1) step_y1 = step_y1 + up_down(rand_xy1) P1.append([step_x1, step_y1]) rand_xy2 = np.random.random_integers(0,3) step_x2 = step_x2 + right_left(rand_xy2) step_y2 = step_y2 + up_down(rand_xy2) P2.append([step_x2, step_y2]) W2d_1 = list_plot(P1, color = "red", plotjoined = True, axes = False) W2d_2 = list_plot(P2, color = "black", plotjoined = True, axes = False) W2d_1 + W2d_2