Two-dimensional [random walk] using random numbers (https://ja.wikipedia.org/wiki/%E3%83%A9%E3%83%B3%E3%83%80%E3%83%A0%E3% 82% A6% E3% 82% A9% E3% 83% BC% E3% 82% AF) is simulated.
One step is set to 1, starting from the origin (0,0) and randomly walking on a two-dimensional plane.
import numpy as np
from random import random
import matplotlib.pyplot as plt
from math import *
"""
2D random walk
"""
N_calc_list = [10]
x, y, r = 0, 0, 0
R_list=[]
N=100000
x_list=[0]
y_list=[0]
for n in range(N):
theta=2.0*pi*random()#Angle θ(2pi unit)To randomize()With[0,1]Generates a uniform random number.
x = x+cos(theta) #Move in the x direction. cos(θ)。
y = y+sin(theta) #Move in the y direction. sin(θ)
x_list.append (x) #The value of x is x_Store in list
y_list.append(y) #The value of y is x_Store in list
# for plot
plt.plot( x_list,y_list) # (x,y)Plot
plt.xlabel('X ') #x-axis label
plt.ylabel('Y') #y-axis label
plt.xlim([-120,120]) #x-axis range
plt.ylim([-120,120]) #y-axis range
plt.show()
With (0,0) as the start position, the trajectory of 100000 steps
The relationship between the distance R from the origin (0,0) and the number of moving steps N. The x-axis is plotted as $ N ^ {1/2} $. The orange line is the theoretical value obtained by $ N ^ {1/2}-> ∞ $.