4
$\begingroup$

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
$\endgroup$
6
  • 1
    $\begingroup$ Welcome to MMA.SE! Please have a look at the FAQ and try to format your code (see Markdown help ). $\endgroup$
    – Yves Klett
    Feb 7, 2013 at 9:11
  • 2
    $\begingroup$ 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? $\endgroup$
    – Yves Klett
    Feb 7, 2013 at 9:58
  • $\begingroup$ These are essentially duplicates and contain good solutions: mathematica.stackexchange.com/questions/5470/… and mathematica.stackexchange.com/questions/13113/…. $\endgroup$
    – whuber
    Feb 7, 2013 at 17:26
  • 2
    $\begingroup$ @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. $\endgroup$
    – whuber
    Feb 8, 2013 at 16:19
  • 1
    $\begingroup$ @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... $\endgroup$
    – Yves Klett
    Feb 8, 2013 at 21:49

3 Answers 3

6
$\begingroup$

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

$\endgroup$
0
3
$\begingroup$

How about

n=1000;
start={{0.,0.,0.}}
distances=Normalize /@ RandomReal[{-1,1}, {n, 3}];
coordinates=Accumulate[Join[start,distances]];
$\endgroup$
10
  • $\begingroup$ Your path is pointing in the (1,1,1) direction. $\endgroup$
    – VLC
    Feb 7, 2013 at 9:44
  • $\begingroup$ RandomReal[{-1,1}, {n, 3}] or similar? $\endgroup$
    – Yves Klett
    Feb 7, 2013 at 9:48
  • 1
    $\begingroup$ @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. $\endgroup$
    – VLC
    Feb 7, 2013 at 13:35
  • 2
    $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Feb 8, 2013 at 2:53
  • 2
    $\begingroup$ @Yves, as VLC notes, you end up with a somewhat biased random walk if you use the RandomReal[] construction. $\endgroup$
    – user5844
    Feb 8, 2013 at 15:08
2
$\begingroup$
 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

$\endgroup$
2
  • $\begingroup$ ahh... "within" vs. "with" unit distance... $\endgroup$
    – Yves Klett
    Feb 7, 2013 at 12:01
  • $\begingroup$ @Yves, right :) $\endgroup$
    – kglr
    Feb 7, 2013 at 12:04

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.