# Generating a random walk with defined step size

By the following I'm trying to generate a list of random coordinates (ex. 4) each within unit distance from previous one, starting from origin. What am I doing wrong?

o = {0, 0, 0};

RP = Module[{},
RN = RandomReal[1000, 3];
RV1 = RN/(2*RootMeanSquare[RN]);
o = RV1 + o;
RV2 = {};
RV2 = Append[RV2, o]
]

In[231]:= Do[RP, {i, 1, 4}]
RV2

• Welcome to MMA.SE! Please have a look at the FAQ and try to format your code (see Markdown help ). – Yves Klett Feb 7 '13 at 9:11
• Your code does contain quite a few problematic/ inconsistent bits, e.g. the Module is essentially useless and you should be using := instead of =, you reset RV each time and Do does not work like you might be assuming. Please read up on the help tutorials and e.g. this thread What are the most common pitfalls awaiting new users? – Yves Klett Feb 7 '13 at 9:58
• These are essentially duplicates and contain good solutions: mathematica.stackexchange.com/questions/5470/… and mathematica.stackexchange.com/questions/13113/…. – whuber Feb 7 '13 at 17:26
• @Yves I hear you. On the other hand, questions of the form "here's my code, please find my mistake" are not acceptable anywhere on SE. Evidently we are a kind and tolerant community. It appears to me that the answers emerging here do not augment the existing answers in any material way. – whuber Feb 8 '13 at 16:19
• @whuber agreed. I still think it might be useful in the sense of a basic, no-frill random walk question if rephrased. The other questions are much more elaborate... – Yves Klett Feb 8 '13 at 21:49

Since the origin is defined with $x$, $y$ and $z$ coordinates, I guess that the random list should be in 3D. Some good hints can be found on this page.

To implement the path in 3D space with steps equal to unit vectors we can take this approach:

rand3Ddir = #*Normalize@RandomReal[NormalDistribution[], 3] &;
origin = {0, 0, 0};
steps = Prepend[Table[rand3Ddir[1], {100}], origin];
path = Accumulate[steps];
Graphics3D[{Line[path], PointSize[Large], Red, Point[path]}]


n=1000;
start={{0.,0.,0.}}
distances=Normalize /@ RandomReal[{-1,1}, {n, 3}];
coordinates=Accumulate[Join[start,distances]];

• Your path is pointing in the (1,1,1) direction. – VLC Feb 7 '13 at 9:44
• RandomReal[{-1,1}, {n, 3}] or similar? – Yves Klett Feb 7 '13 at 9:48
• @YvesKlett You're right, but since I don't know what the application of this 3D random walk will be, I just wanted to note that there are some subtle differences between the two approaches. – VLC Feb 7 '13 at 13:35
• Actually there's quite an important difference. RandomReal[{-1,1}, {n, 3}] gives vectors uniformly distributed in a cube. The direction of the vectors is not uniformly distributed on a sphere. But it's true that it won't change the random walk much. – Szabolcs Feb 8 '13 at 2:53
• @Yves, as VLC notes, you end up with a somewhat biased random walk if you use the RandomReal[] construction. – user5844 Feb 8 '13 at 15:08
 rndmwlk = NestList[# +  RandomReal[1] Normalize[RandomReal[{-1, 1}, {3}]] &,
{0., 0.,  0.}, 100];

Graphics3D[{Tube@rndmwlk, PointSize[.04], Black, Sphere[rndmwlk[[1]], .15],
{Hue[RandomReal[]], Sphere[#, .1]} & /@ rndmwlk[[2 ;;]]}, BoxRatios -> 1]


• ahh... "within" vs. "with" unit distance... – Yves Klett Feb 7 '13 at 12:01
• @Yves, right :) – kglr Feb 7 '13 at 12:04