5
$\begingroup$

I am new to mathematica, and I have this 3d random walker program which runs with a fixed (given) amount of steps. What I want it to do is to run forever but stop when two walkers collide (aka have the same position). When it stops it should return the number of steps before the collision. So I am planning on having that function to be in loop which in Java/C might look similar to this:

for (i = 0; i < times_to_run; i++) 
{ 
    total = total + randomWalker(args..);
}


Clear[randomWalk3D]
randomWalk3D[steps_Integer, start_, region_] /;
  start \[Element] region := 

  ...

  x = 0; v = {}; For[i = 1, i < steps, i++,
     If[{positions[[i]]} == {positions2[[i]]}, x++; AppendTo[v, i],]

    ]
   Manipulate [
    Graphics3D[{Opacity[0.5, Gray], region, AbsolutePointSize[10], 
      Cyan, Line[positions], Red, Line[positions2], 
      pointPrimitives[i], pointPrimitives2[i]}, text[i], 
     ImagePadding -> 5, Lighting -> {{"Ambient", Gray}}], {i, 1, 
     Length[positions], 1}]
   Print["Number of collisions: ", x]
   Print["Collision at i = ", v]

  ]
randomWalk3D[1000, {4, 4, 4}, Cuboid[{0, 0, 0}, {10, 10, 10}]]

https://github.com/tabilzad/Random/blob/master/RandomW.nb

$\endgroup$
  • $\begingroup$ Please post your mathematica code $\endgroup$ – Dr. belisarius Dec 29 '15 at 3:21
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Dr. belisarius Dec 29 '15 at 3:21
  • $\begingroup$ Do you want the number of steps only or an actual animation? $\endgroup$ – JungHwan Min Dec 29 '15 at 3:21
  • $\begingroup$ Thank you for a quick reply! I posted the link to my code. I don't care so much about the animation... but numbers is whats more important $\endgroup$ – Casper Dec 29 '15 at 3:26
  • $\begingroup$ @Dr.belisarius see above $\endgroup$ – Casper Dec 29 '15 at 3:34
9
$\begingroup$

In 2D:

SeedRandom[3];
pos1 = {0, 0};
pos2 = {4, 4};
rc := RandomChoice[{-1, 0, 1}, {2, 2}]
l = Transpose@NestWhileList[# + rc &, {pos1, pos2}, Unequal @@ # &];
Print@Length@l[[1]];
ListLinePlot@l
(*32*)

Mathematica graphics


Perhaps the following is better for visualization

graph[s_, col_] := Module[{rul, edges},
  rul = Thread[Rule[Union@s, Range@Length@Union@s]];
  edges = Rule @@@ (Partition[s, 2, 1] /. rul); 
  GraphPlot[edges, VertexCoordinateRules -> Reverse /@ rul, MultiedgeStyle -> 1/7, 
   EdgeRenderingFunction -> ({col, Arrowheads[Small], Arrow[#1]} &)]]

Show @@ MapThread[graph, {l, {Red, Green}}, 1]

enter image description here

|improve this answer|||||
$\endgroup$

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.