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$
9
  • $\begingroup$ Please post your mathematica code $\endgroup$ Commented Dec 29, 2015 at 3:21
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) Browse common pitfalls. 2) As you receive help, try to give it too, by answering questions in your area of expertise. 3) Take the tour and check the help center! 4) When you see good questions and answers, vote them up using the triangles. Also, please remember to accept an answer if it solves your problem, by clicking the checkmark! $\endgroup$ Commented Dec 29, 2015 at 3:21
  • $\begingroup$ Do you want the number of steps only or an actual animation? $\endgroup$ Commented Dec 29, 2015 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$ Commented Dec 29, 2015 at 3:26
  • $\begingroup$ @Dr.belisarius see above $\endgroup$ Commented Dec 29, 2015 at 3:34

1 Answer 1

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

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.