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

• 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! Dec 29 '15 at 3:21
• Do you want the number of steps only or an actual animation? Dec 29 '15 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 Dec 29 '15 at 3:26
• @Dr.belisarius see above Dec 29 '15 at 3:34

In 2D:

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

graph[s_, col_] := Module[{rul, edges}, 