(* 1D random walk for 1 walker *)
(*Initialisation*)
nlast=10000;
x[0]=0;sumx=0;sumxsq=0;
(*End of Initialisation*)

For[n=1,n≤nlast,n++,
(*ran=Random[Integer,{-1,1}];*)
ran=Random[Real];
If[
ran<0.5,x[n]=x[n-1]-1,x[n]=x[n-1]+1;
];(*end if *)
sumx=sumx+x[n];
avex[n]=sumx/(n+1);
sumxsq=N[sumxsq+(x[n]^2)];
avexsq[n]=sumxsq/(n+1);
];(*end for n*)
tnx=Table[{n,x[n]},{n,0,nlast}];
tnavex=Table[{n,avex[n]},{n,0,nlast}];
ListPlot[tnx,Joined->True]
ListPlot[tnavex,Joined->True]

(* Random walk of a collection of jlast walkers in 1D *)

(*Initialisation*)
nlast=500;jlast=2000;(*total number of walkers*)
L=100;L1=-L;L2=L;
xc=IntegerPart[(L1+L2)/2];

For[j=1,j≤jlast,j++,
x[j]=xc;
];(*end of j*)
avex[0]=Sum[x[j],{j,1,jlast}]/jlast;
avexsq[0]=Sum[x[j]^2,{j,1,jlast}]/jlast;
tnx[0]=Table[{x[j],0},{j,1,jlast}];
tx[0]=Table[x[j],{j,1,jlast}];
(*End of Initialisation*)

For[n=1,n≤nlast,n++,
For[j=1,j≤jlast,j++,
ran=Random[Real];
If[ran≤0.5,x[j]=x[j]-1
,(*else*)
x[j]=x[j]+1
](*end if *)
];(*end for j*)

tnx[n]=Table[{x[j],0},{j,1,jlast}];
tx[n]=Table[x[j],{j,1,jlast}];

(*evaluate the average of x[j] and xsq[j] at fixed n*)
avex[n]=Sum[x[j],{j,1,jlast}]/jlast;
avexsq[n]=Sum[x[j]^2,{j,1,jlast}]/jlast;
(*evaluate the average of x[j] and xsq[j] at fixed n*)
];(*end for n *)
tnavex=Table[{n,avex[n]},{n,0,nlast}];
tnavexsq=Table[{n,avexsq[n]},{n,0,nlast}];
lptnavexsq=ListPlot[tnavexsq,PlotLabel->"avexsq, vs n"];

yth[n_]:=2*Dx*n;Dx=0.5;
tny=Table[{n,yth[n]},{n,0,nlast}];
lptny=ListPlot[tny,PlotLabel->"yth vs n",PlotStyle->Red];
Show[{lptny,lptnavexsq}];
(*Manipulate[
ListPlot[tnx[n],PlotRange->{{L1,L2},{-1,1}}],
{n,0,nlast,1}]*)
Manipulate[Show[Histogram[tx[n],50]],{n,0,nlast,1}];

Manipulate[
ListPlot[N[BinCounts[tx[n],{L1,L2,2}]/jlast],PlotRange->{0,1},Filling->Axis]
,{n,0,nlast,1}]

(* Example of binning *)
rand[i_]:=Random[];
tran=Table[rand[i],{i,1,1000}];

(*Histogram[tran,50,"ProbabilityDensity"]*)
Histogram[tran,50]

(*2D random walk*)

(*Initialisation*)
nlast=500;ilast=jlast=500;(*total number of walkers*)
L=100;L1=-L;L2=L;
xc=IntegerPart[(L1+L2)/2];

For[j=1,j≤jlast,j++,
y[j]=xc;
For[i=1,i≤ilast,i++,
x[i]=xc;
];(*end of j*)
];(*end of j*)

avex[0]=Sum[x[j],{j,1,jlast}]/jlast;
avexsq[0]=Sum[x[j]^2,{j,1,jlast}]/jlast;

avey[0]=Sum[y[j],{j,1,jlast}]/jlast;
aveysq[0]=Sum[y[j]^2,{j,1,jlast}]/jlast;

tnx[0]=Table[{x[j],0},{j,1,jlast}];
tx[0]=Table[x[j],{j,1,jlast}];
tny[0]=Table[{y[j],0},{j,1,jlast}];
ty[0]=Table[y[j],{j,1,jlast}];

tnr[0]=Table[Sqrt[x[j]^2+y[j]^2],{j,1,jlast}];

tnr2[0]=Table[{x[j],y[j]},{j,1,jlast}];
(*End of Initialisation*)

For[n=1,n≤nlast,n++,

For[j=1,j≤jlast,j++,
ran=Random[Real];
If[ran≤0.25,x[j]=x[j]-1];(*end if *)
If[0.25<=ran≤0.5,x[j]=x[j]+1];(*end if *)
If[0.5<=ran≤0.75,y[j]=y[j]+1];(*end if *)
If[0.75<=ran≤1.0,y[j]=y[j]-1];(*end if *)
r[j]=Sqrt[x[j]^2+y[j]^2]
];(*end for j*)

tnr[n]=Table[r[j],{j,1,jlast}];
tnr2[n]=Table[{x[j],y[j]},{j,1,jlast}];

(*evaluate the average of r[j] and rsq[j] at fixed n*)
aver[n]=Sum[r[j],{j,1,jlast}]/jlast;
aversq[n]=Sum[r[j]^2,{j,1,jlast}]/jlast;
(*evaluate the average of r[j] and rsq[j] at fixed n*)
];(*end for n *)
tnaver=Table[{n,aver[n]},{n,0,nlast}];
tnaversq=Table[{n,aversq[n]},{n,0,nlast}];

Manipulate[
ListPlot[tnr2[n],AspectRatio->1,PlotRange->{{L1,L2},{L1,L2}}],
{n,0,nlast,1}]

lptnaversq=ListPlot[tnaversq,PlotLabel->"aversq, vs n"];
yth[n_]:=2*Dx*n;Dx=0.5;
tny=Table[{n,yth[n]},{n,0,nlast}];
lptny=ListPlot[tny,PlotLabel->"yth vs n",PlotStyle->Red];
Show[{lptny,lptnaversq}]


(*
Manipulate[
ListPlot[N[BinCounts[tnr[n],{L1,L2,2}]/jlast],PlotRange->{0,1},Filling->Axis]
,{n,0,nlast,1}]
*)