# 3D random walk between two end points

Say I have 2 endpoints A and B. I would like to use W. Mathematica to generate a random walk between these two endpoints in a three-dimensional space. Es.: A=Point[{0,0,0}] and B=Point[{1,1,1}]. I would like to have as an output the random trajectory of the particle. How can I do this? If I use

Line[Accumulate[RandomChoice[{-1, 1}, {1000, 3}]]]


As suggested in the documentation, I would have to complete each output with a trajectory segment that would bring the particle to point B. Yet in this way the last segment may be "long" and not "enough" random. How can I avoid that or simply generate a random trajectory that links two points?

• Would those down voting my question please leave some comments? – Mirko Aveta Apr 6 '17 at 10:35
• You could generate a random walk in 3D and then transform (translate, rotate, stretch?) it to start and end at the given points. I leave you the task of working out whether its statistics are good enough for your application. (Perhaps you want to use NormalDistribution steps so that it is valid to rotate?) – mikado Apr 6 '17 at 11:46
• The difficulty is not on Mathematica programming, but to select an appropriate algorithm for targeted random walk. There are papers on this topic, if you specify an algorithm, then it is easy to proceed. – vapor Apr 6 '17 at 13:20
• Follow @happyfish 's comments and look for "Brownian bridge". – JimB Apr 6 '17 at 15:21
• See this. – J. M.'s torpor Apr 7 '17 at 5:40

Here's one possible way using the function BrownianBridgeProcess and RandomFunction (you will have to check to make sure it's statistically what you need - in particular the motion in each dimensions is independent of the others).

walk[a_List, b_List, n_] :=
Thread[First@RandomFunction[#, {0, 1, 1/n}]["ValueList"] & /@
MapThread[BrownianBridgeProcess[{0, #1}, {1, #2}] &, {a, b}]]


Here you specify a starting point, an ending point, and the number of steps to take between them.

a = {0, 0, 0};
b = {1, 1, 1};
Graphics3D[{Black, Arrow[Tube@walk[a, b, 100]], PointSize[Large],
Red, Point /@ {a, b}}, Axes -> True] a = {4, 10, 2};
b = {-3, 12, 0};
Graphics3D[{Black, Arrow[Tube@walk[a, b, 100]], PointSize[Large],
Red, Point /@ {a, b}}, Axes -> True] Another very slow approach.

Create a random walk trajectory and then take all the pairs whose distance is the one you need

l = Accumulate[RandomChoice[{-1, 1}, {1000, 3}]];
Graphics3D@Line[l]

Show[Graphics3D@Line[l],
If[EuclideanDistance[#[], #[]] == 25 Sqrt,
Graphics3D@{Red, Line[#]}, Nothing] & /@ Subsets[l, {2}]] All those pairs have the distance you want and can use it for statistical purposes. Subsets goes as n^2 so it's very slow.

• You can find the same number pairs quickly if you do it numerically instead of symbolically, compare dist = DistanceMatrix[l, l]; with l = Accumulate[RandomChoice[{-1, 1}, {1000, 3}]]; and l = Accumulate[RandomChoice[{-1., 1.}, {1000, 3}]]; respectively. The pairs can be found by Position[dist, _?(# - 43.3 < 0.01 &), {2}] and other methods (Pick, Unitize etc. is probably best for speed). – C. E. Apr 6 '17 at 21:18
• @C.E. I imagined there were faster ways but I'm not to used to comfortable with Pick et al. Didn't know about DistanceMatrix either, thanks! – tsuresuregusa Apr 6 '17 at 23:28

So for a target of $2n+1$ moves, for each dimension, generate a list of $n$ moves on the "-1" direction and $n+1$ "1" moves and then scramble the list using RandomSample[ ]. Then combine the moves in each dimension.