Random Walk in 1D

Preliminaries

Clear all symbols from previous evaluations to avoid conflicts

Clear["Global`*"]

Simple walk in 1D - arbitrary step of maximal length d

Start at origin and make 10000 random steps:

p=10; (* number of walks *)
n=10000;(* number of steps *)
d=1.0;(* maximal length of each step *)
X=ConstantArray[0.0,{p,n+1}];
(* SeedRandom[2021] *)
Do[
Do[
X[[ip,in+1]]=X[[ip,in]]+d * RandomReal[{-1.0,1.0}],
{in,1,n}
],
{ip,1,p}
];
ListPlot[X,Joined->{True}]

Take p random walks for each number of steps n (up to nmax) and calculate the mean distance from the origin

(* SeedRandom[2021] *)
nmax=100;(* maximal number of steps *)
p=1000;(* number of walks to average *)
d=1.0;(* maximal length of each step *)
(* array for averaging positions for each number of steps *)
Pc=ConstantArray[0.0,nmax];
(* array for calculating standard deviation of positions from origin for each number of steps *)
Pc2=ConstantArray[0.0,nmax];
(* array for averaging distances from origin for each number of steps *)
Lc=ConstantArray[0.0,nmax];
(* array for calculating standard deviation of distances from origin for each number of steps *)
Lc2=ConstantArray[0.0,nmax];
Do[
Do[
x=0.0;
Do[
x+=d * RandomReal[{-1.0,1.0}],
{in,1,n}
];
Pc[[n]]+=x;
Lc[[n]]+=Abs[x];
Lc2[[n]]+=x^2,
{ip,1,p}
];
Pc[[n]]=Pc[[n]]/p;
Pc2[[n]]=Sqrt[Lc2[[n]]/p-Pc[[n]]^2];
Lc[[n]]=Lc[[n]]/p;
Lc2[[n]]=Sqrt[Lc2[[n]]/p-Lc[[n]]^2],
{n,1,nmax}
];
ListPlot[{Pc,Lc},Joined->{False}]

fnPσ=Array[d*(#/3)^(1/2)&,nmax];
ListPlot[{Pc,Pc+Pc2,Pc-Pc2,fnPσ},
Joined->{False,False,False,True},
PlotStyle->{Blue,Green,Green,Red},
PlotLegends->{"Mean position P","P + standard deviation","P - standard deviation","d*(n/3)^(1/2)"}]

{cc,αc}={a,b}/.FindFit[Lc,a*y^b,{a,b},y];
Print["Parameters of fitting c*n^α: c = ",cc,", α = ",αc]
fnc=Array[cc*#^αc&,nmax];
ListPlot[{Lc,Lc+Lc2,Lc-Lc2,fnc},
Joined->{False,False,False,True},
PlotStyle->{Blue,Green,Green,Red},
PlotLegends->{"Mean distance L","L + standard deviation","L - standard deviation","c*n^α"}]

Simple walk in 1D - discrete steps (d = 1) of random direction

Start at origin and make 10000 random steps:

p=10; (* number of walks *)
n=10000;(* number of steps *)
d=1.0;(* maximal length of each step *)
X=ConstantArray[0.0,{p,n+1}];
(* SeedRandom[2021] *)
Do[
Do[
X[[ip,in+1]]=X[[ip,in]]+d * RandomChoice[{-1,1}],
{in,1,n}
],
{ip,1,p}
];
ListPlot[X,Joined->{True}]

Take p random walks for each number of steps n (up to nmax) and calculate the mean distance from the origin

(* SeedRandom[2021] *)
nmax=100;(* maximal number of steps *)
p=1000;(* number of walks to average *)
d=1.0;(* maximal length of each step *)
(* array for averaging positions for each number of steps *)
P=ConstantArray[0.0,nmax];
(* array for calculating standard deviation of positions from origin for each number of steps *)
P2=ConstantArray[0.0,nmax];
(* array for averaging distances from origin for each number of steps *)
L=ConstantArray[0.0,nmax];
(* array for calculating standard deviation of distances from origin for each number of steps *)
L2=ConstantArray[0.0,nmax];
Do[
Do[
x=0.0;
Do[
x+=d * RandomChoice[{-1,1}],
{in,1,n}
];
P[[n]]+=x;
L[[n]]+=Abs[x];
L2[[n]]+=x^2,
{ip,1,p}
];
P[[n]]=P[[n]]/p;
P2[[n]]=Sqrt[L2[[n]]/p-P[[n]]^2];
L[[n]]=L[[n]]/p;
L2[[n]]=Sqrt[L2[[n]]/p-L[[n]]^2],
{n,1,nmax}
];
ListPlot[{P,L},Joined->{False}]

fnPσ=Array[d*(#)^(1/2)&,nmax];
ListPlot[{P,P+P2,P-P2,fnPσ},
Joined->{False,False,False,True},
PlotStyle->{Blue,Green,Green,Red},
PlotLegends->{"Mean position P","P + standard deviation","P - standard deviation","d*(n)^(1/2)"}]

{c,α}={a,b}/.FindFit[L,a*y^b,{a,b},y];
Print["Parameters of fitting c*n^α: c = ",c,", α = ",α]
fn=Array[c*#^α&,nmax];
ListPlot[{L,L+L2,L-L2,fn,fnc},
Joined->{False,False,False,True,True},
PlotStyle->{Blue,Green,Green,Red,Orange},
PlotLegends->{"Mean distance L","L + standard deviation","L - standard deviation","c*n^α","non-discrete case"}]