# How to make my fuction return a step count

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}]]

• Please post your mathematica code Commented Dec 29, 2015 at 3:21
• 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! Commented Dec 29, 2015 at 3:21
• Do you want the number of steps only or an actual animation? Commented Dec 29, 2015 at 3:21
• 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 Commented Dec 29, 2015 at 3:26
• @Dr.belisarius see above Commented Dec 29, 2015 at 3:34

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*)


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]