0

How do i generate a RandomWalk with n dimensions?

I already have some examples for a 1 or 2 dimensional random walk but now i have to code a random walk with n dimensions and i seriously don't know how.

here are 2 examples for 2 dimensions:

 RandomWalk2DLattice[n_] := 
 Accumulate[Through[{Cos, Sin}[# \[Pi]/2]] & /@ RandomInteger[3, {n}]]
 rw = RandomWalk2DLattice[500];

 Show[Graphics[{Line[rw], {PointSize[.02], Point[rw[[{-1}]]], 
        Point[{0, 0}]}}, Axes -> True], AspectRatio -> Automatic]

Does anyone have an answer?

  • 1
    Hint: Look at https://reference.wolfram.com/language/ref/RandomInteger.html and think of the various ways you can give it parameters. That should be enough. – Bill Apr 28 '16 at 15:51

1 Answers1

1
ndim = 3;
walk = NestList[(d = RandomInteger[{1, ndim}]; 
         ReplacePart[#, d -> #[[d]] + RandomChoice[{-1, 1}]]) &,
                ConstantArray[0, ndim], 1000];

enter image description here

agentp
  • 6,849
  • 2
  • 19
  • 37