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?

  • 2
    $\begingroup$ Would those down voting my question please leave some comments? $\endgroup$ Commented Apr 6, 2017 at 10:35
  • $\begingroup$ 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?) $\endgroup$
    – mikado
    Commented Apr 6, 2017 at 11:46
  • 5
    $\begingroup$ 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. $\endgroup$
    – vapor
    Commented Apr 6, 2017 at 13:20
  • 4
    $\begingroup$ Follow @happyfish 's comments and look for "Brownian bridge". $\endgroup$
    – JimB
    Commented Apr 6, 2017 at 15:21
  • 1
    $\begingroup$ See this. $\endgroup$ Commented Apr 7, 2017 at 5:40

3 Answers 3


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]

Mathematica graphics

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

Mathematica graphics


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}]];

 If[EuclideanDistance[#[[1]], #[[2]]] == 25 Sqrt[3], 
    Graphics3D@{Red, Line[#]}, Nothing] & /@ Subsets[l, {2}]]

enter image description here

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.

  • 3
    $\begingroup$ 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). $\endgroup$
    – C. E.
    Commented Apr 6, 2017 at 21:18
  • $\begingroup$ @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! $\endgroup$ Commented Apr 6, 2017 at 23:28

I'd leave a comment, but not enough reputation, so here is a partial answer.

Assuming step size is 1 or -1 in each direction, then in each direction you will end up with zero or more {1,-1} pairs plus a "1" move. This means you will always have an odd number of moves, with one more in the "1" direction than in the "-1" direction.

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.

This allows for earlier visits to the {1,1,1} point, but you didn't say that wasn't OK.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.