# Periodic boundary conditions for two walkers

For some reason I'm getting infinite loops most of the time i run this. Perhaps the periodic boundary conditions are wrong. Seems like the probability of the two positions to be the same is extremely low, it shouldn't be.

    stepTypes = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
pos1 = {RandomInteger[{0, 4}], RandomInteger[{0, 4}]};
pos2 = {RandomInteger[{0, 4}], RandomInteger[{0, 4}]};
While[pos1 != pos2,
step = RandomChoice[stepTypes];
step2 = RandomChoice[stepTypes];

pos1 = pos1 + step;

Which[First[pos1] < 0, pos1 = pos1 + {4, 0}, First[pos1] > 4,
pos1 = pos1 - {4, 0}];
Which[Last[pos1] < 0, pos1 = pos1 + {0, 4}, Last[pos1] > 4,
pos1 = pos1 - {0, 4}];

pos2 = pos2 + step2;
Which[First[pos2] < 0, pos2 = pos2 + {4, 0}, First[pos2] > 4,
pos2 = pos2 - {4, 0}];
Which[Last[pos2] < 0, pos2 = pos2 + {0, 4} , Last[pos2] > 4,
pos2 = pos2 - {0, 4} ];
Print[pos1, pos2];
]


Does this do what you want?

stepTypes = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
{pos1, pos2} = RandomInteger[{0, 4}, {2, 2}];
While[pos1 != pos2,
{pos1, pos2} = Mod[{pos1, pos2} + RandomChoice[stepTypes, 2], 5];
Print[pos1, pos2];]

• Thank you, this seem to work a lot faster. But if i change the range to say 'RandomInteger[{0, 5}, 2]; ' and Mod[..,6] it still runs infinitely, please correct me if i am wrong. – Casper Apr 11 '16 at 4:45
• seems like the first number has to be even and the mod number has to be odd....? – Casper Apr 11 '16 at 4:47
• also with my Which logic if the position is to be outside of the range, say {0,5} - I subtract {0,4} which makes it {0,1}, but the mod logic would make it {0,0}. Is there a way to implement that? – Casper Apr 11 '16 at 4:53
• I think the "infinite run" may be a kind of polarity issue. Consider that by your definition of stepTypes, each agent must move +/-1 in a direction. Depending on the starting position, it may be that it's impossible for them to align. This can happen even in 1-d. Imagine a grid of size 2 (just zero and one) in mod 2. Now each must move one step, and one moves to the left and one moves to the right, and they can never occupy the same place. – bill s Apr 11 '16 at 14:03
• Anyway to make the two walkers run in parallel? I know that the random generators that Wolfram uses by default for parallel computations are different from the one used for serial computation – Casper Apr 11 '16 at 17:09