4

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
Yves Klett
  • 15,383
  • 5
  • 57
  • 124
Sesna Secna
  • 169
  • 8
  • 1
    Welcome to MMA.SE! Please have a look at the FAQ and try to format your code (see Markdown help ). – Yves Klett Feb 07 '13 at 09: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 07 '13 at 09:58
  • These are essentially duplicates and contain good solutions: http://mathematica.stackexchange.com/questions/5470/speeding-up-random-walk-for-many-particles and http://mathematica.stackexchange.com/questions/13113/3d-random-walk-with-periodic-boundary-conditions. – whuber Feb 07 '13 at 17:26
  • @whuber although the question "What am I doing wrong" is not properly answered yet – Yves Klett Feb 08 '13 at 16:14
  • 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 08 '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 08 '13 at 21:49

3 Answers3

6

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

VLC
  • 9,818
  • 1
  • 31
  • 60
3

How about

n=1000;
start={{0.,0.,0.}}
distances=Normalize /@ RandomReal[{-1,1}, {n, 3}];
coordinates=Accumulate[Join[start,distances]];
Thomas
  • 1,967
  • 1
  • 15
  • 18
  • Your path is pointing in the (1,1,1) direction. – VLC Feb 07 '13 at 09:44
  • RandomReal[{-1,1}, {n, 3}] or similar? – Yves Klett Feb 07 '13 at 09:48
  • Indeed! Corrected the post. – Thomas Feb 07 '13 at 10:43
  • Althogh... the OP also used a positive range, perhaps not wholly intentionally ;-) – Yves Klett Feb 07 '13 at 10:55
  • Note the difference between using Normalize @ RandomReal[{-1, 1}, 3] and Normalize@RandomReal[NormalDistribution[], 3] in this post. – VLC Feb 07 '13 at 12:01
  • @VLC for the novice user NormalDistribution may be a bit advanced / frightening. – Yves Klett Feb 07 '13 at 12:06
  • 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 07 '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 08 '13 at 02:53
  • 2
    @Yves, as VLC notes, you end up with a somewhat biased random walk if you use the RandomReal[] construction. –  Feb 08 '13 at 15:08
  • @JuanJoder, good point. You could go for spherical coordinates, but then the solution is not so concise. – Yves Klett Feb 08 '13 at 15:13
2
 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

kglr
  • 394,356
  • 18
  • 477
  • 896