5
$\begingroup$

I'm new to Mathematica and still teaching all by myself, hope you bear with me.

I have the code:

For[i = 1; t = 1, i <= 10, i++, t = t + RandomChoice[{-1, 0, 1}]; 
Print[t]]

When I evaluate it I and get different outputs at each evaluation, as expected. Here is a sample output:

enter image description here

You can imagine it as a simple random walk on an integer line with a particle starting at integer 1. It either moves to the right (+1), left (-1) or stays (0).

What I want to do is make a random walk with two different particles. Let's say particle A starts at 2 and particle B starts at -2. I want both particles to start their walk and stop when they meet. Meeting does not have to mean that they stop at a common point; being one step away, like particle A is at 0 and particle B is at -1, will do.

Can I still use the code I made and combine it of some way with other code to do that? Any help is appreciated. It's okay if there's no graphics.

$\endgroup$
  • 1
    $\begingroup$ I have edited my answer. There are things to do after your question is answered. But wait! It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. Look how Mr.Wizard improved my answer, when he could have posted his own. New users should test answers before voting and wait 24 hours before accepting the best one. Participation is essential for the site, please do your part. $\endgroup$ – rhermans Jul 29 '18 at 15:16
10
$\begingroup$

First you should read Why should I avoid the For loop in Mathematica? and also What are the most common pitfalls awaiting new users? so to learn, among many other useful tips, why you shouldn't have used Print .

This was my first answer (which is later improved)

SeedRandom[10];
rwalk = NestWhileList[
   (RandomChoice[{-1, 0, 1}, 2] + #) &,
   {-2, 2},
   Not[Equal @@ #] &
   ];

ListPlot[
 Transpose@rwalk
 , Joined -> True
 , PlotTheme -> "Scientific"
 ]

Mathematica graphics

But be aware that for some runs you could run out of memory because the two paths may never meet. Therefore a better solution would be to use a maximum number of iterations in NestWhileList.

Also, as per M.Wizard's comment you can also use DuplicateFreeQ instead of Not[Equal @@ #] &.

So an improved solution, with a maximum of 1 million iterations

SeedRandom[10];
rwalk = NestWhileList[
   (RandomChoice[{-1, 0, 1}, 2] + #) &,
   {-2, 2},
   DuplicateFreeQ,
   1,
   10^6
   ];

It was also suggested to use InterpolationOrder -> 0, which I think is debatable when this is physically the best representation. This would not be my personal choice in general, but anyhow here it goes:

ListPlot[
 Transpose@rwalk
 , Joined -> True
 , PlotTheme -> "Scientific"
 , InterpolationOrder -> 0
 ]

Mathematica graphics

$\endgroup$
  • 3
    $\begingroup$ DuplicateFreeQ might be a stand-in for Not[Equal @@ #] & $\endgroup$ – Mr.Wizard Jul 29 '18 at 14:36
  • $\begingroup$ Adding InterpolationOrder -> 0 into ListPlot might be more informative. $\endgroup$ – OkkesDulgerci Jul 29 '18 at 14:41
  • 3
    $\begingroup$ One more note: you can use the fifth parameter of NestWhileList to abort after a given number of steps. I'm sure you know this but Adrian probably doesn't yet. $\endgroup$ – Mr.Wizard Jul 29 '18 at 14:47
  • $\begingroup$ wow, that's awesome thank you! Information about avoiding for loops is great too $\endgroup$ – mister_bintots Jul 29 '18 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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