I am working on a 3D random walk with periodic boundary conditions and have written a program that will do this for me, but it is extremely slow. Does anyone have any suggestions on how I could speed this up?
Timing[
n = 1.;
T = 10000.;
boundary = 5;
initial = boundary;
step = 0.1;
RandomWalk[x_] :=
Accumulate[
Join[{Table[RandomReal[{-initial, initial}], {3}]},
RandomVariate[NormalDistribution[0, step], {x, 3}]]];
i = 1;
(Label[begin];
p[i] = RandomWalk[T + 1];
If[i == n, Goto[end]];
i = i + 1;
Goto[begin];
Label[end];);
t = 0;
(Label[begin1];
i = 1;
Label[begin2];
positionslist = p[i];
If[p[i][[t + 1, 1]] > boundary,
p[i] = Join[Take[positionslist, t + 1],
Accumulate[
Join[{{-boundary, p[i][[t + 1, 2]], p[i][[t + 1, 3]]}},
RandomVariate[
NormalDistribution[0, step], {T - t + 1, 3}]]]]];
If[p[i][[t + 1, 1]] < -boundary,
p[i] = Join[Take[positionslist, t + 1],
Accumulate[
Join[{{boundary, p[i][[t + 1, 2]], p[i][[t + 1, 3]]}},
RandomVariate[
NormalDistribution[0, step], {T - t + 1, 3}]]]]];
If[p[i][[t + 1, 2]] > boundary,
p[i] = Join[Take[positionslist, t + 1],
Accumulate[
Join[{{p[i][[t + 1, 1]], -boundary, p[i][[t + 1, 3]]}},
RandomVariate[
NormalDistribution[0, step], {T - t + 1, 3}]]]]];
If[p[i][[t + 1, 2]] < -boundary,
p[i] = Join[Take[positionslist, t + 1],
Accumulate[
Join[{{p[i][[t + 1, 1]], boundary, p[i][[t + 1, 3]]}},
RandomVariate[
NormalDistribution[0, step], {T - t + 1, 3}]]]]];
If[p[i][[t + 1, 3]] > boundary,
p[i] = Join[Take[positionslist, t + 1],
Accumulate[
Join[{{p[i][[t + 1, 1]], p[i][[t + 1, 2]], -boundary}},
RandomVariate[
NormalDistribution[0, step], {T - t + 1, 3}]]]]];
If[p[i][[t + 1, 3]] < -boundary,
p[i] = Join[Take[positionslist, t + 1],
Accumulate[
Join[{{p[i][[t + 1, 1]], p[i][[t + 1, 2]], boundary}},
RandomVariate[
NormalDistribution[0, step], {T - t + 1, 3}]]]]];
If[i == n, Goto[end2]];
i = i + 1;
Goto[begin2];
Label[end2];
If[t == T, Goto[end1]];
t = t + 1;
Goto[begin1];
Label[end1];);]
{9.44905, Null}
I would like to do this simulation for multiple particles (at least 50) for about 100000 time steps and not have it take all day.



Mod[]? – J. M.'s missing motivation Oct 16 '12 at 00:43(Label[begin]; p[i] = RandomWalk[T + 1]; If[i == n, Goto[end]]; i = i + 1; Goto[begin]; Label[end];);can surely be done more efficiently as aTablecommand:Table[p[i]=RandomWalk[T+1],{i,n}]. – Verbeia Oct 16 '12 at 00:46FoldListwith aCompile-ed step function. Sorry, no time to post full answer now... – Ajasja Oct 16 '12 at 13:46