I defined a security boundary for a random walk:
p = 5000;(*steps*)
tc = 15;(*cube edge length*)
tp = 0.2;
random = Accumulate[
Join[{RandomReal[{-tc, tc}/2, 3]},
RandomVariate[NormalDistribution[0, tp], {p, 3}]]];
periodizedWalk = Mod[random, tc, -tc/2];
splitPeriodizedWalk =
Split[periodizedWalk, EuclideanDistance[#1, #2] < tc/2 &];
With[{cube = First[PolyhedronData["Cube"]]},
Graphics3D[{{Opacity[0.1], Scale[cube, tc]}, Line[random]},
Boxed -> False]]
and now i want to obtain a sample of the random times at which the random walk crosses the boundary for the first time. Something like this:
W = {}; For[i = 1, i <= 5000, i++,
PosFinal = {0, 0}; EME = 1;
While[True,
PosFinal = PosFinal + step[Random[]];
If[Norm[PosFinal] > 15, Break[]];
EME = EME + 1;
];
W = Append[W, EME];
]
but i want these two codes to be related. Would like some help please :)


Reap/Sowwhile creating data. – Kuba Mar 23 '14 at 11:43W = {}; For[i = 1, i <= 1000, i++, PosFinal = {0, 0}; EME = 1; While[True, PosFinal = PosFinal + step[Random[]]; If[Norm[PosFinal] > 15, Break[]];
EME = EME + 1; ]; W = Append[W, EME]; ]
but i want this code and the first to be related.
– Mariana da Costa Mar 23 '14 at 12:11Sow[new]toReapthose posints at the end. Also, do not useAppendto create lists. See point 3.2 – Kuba Mar 23 '14 at 12:38Sowwith two different tags one for each point and additional one for only crossings :) Or useNestListinstead ofFor. Chow to check, well one point is inside and next one is outside or the opossite. You can use similar solution tu mine from the answer. – Kuba Mar 23 '14 at 13:08Do[ i = 0; NestWhile[(i++; # + RandomReal[{-1, 1}, 3]) &, {0, 0, 0}, Norm[#] < 15 &]; Sow[i], {20}] // Reap– Kuba Mar 23 '14 at 17:07Reap/Sowversion to the solution - just in case - to show to the folks minimal form. Very nice in general, I do enjoy when folks care to add a nice picture ;-) – Vitaliy Kaurov Mar 23 '14 at 19:52