Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
share|improve this question
1  
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
2  
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
2  
@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
1  
@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
show 1 more comment

3 Answers

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

enter image description here

share|improve this answer
add comment

How about

n=1000;
start={{0.,0.,0.}}
distances=Normalize /@ RandomReal[{-1,1}, {n, 3}];
coordinates=Accumulate[Join[start,distances]];
share|improve this answer
    
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
1  
@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
2  
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
2  
@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
show 5 more comments
 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]

enter image description here

share|improve this answer
    
ahh... "within" vs. "with" unit distance... –  Yves Klett Feb 7 '13 at 12:01
    
@Yves, right :) –  kguler Feb 7 '13 at 12:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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