You might consider to use a tuple of directions, something like this:
directions = ((1,0), (0,1), (-1,0), (0,-1))
In your while loop, you can do:
direction = directions[np.random.randint(4)]
A += direction[0]
B += direction[1]
If you switch from tuple to numpy arrays (direction and your current position), you could also use numpy.add which might be faster. If this really improve performence you have to measure. To do this, you might use a seed for your random generator, to get reproducible code.
The border check might be faster with NumPy too, using abslolut - if its ok, to change the behavior that way (your praticles wont "clue" at the border, but bounce back).
Update You could go a step further, by precalculating all posible paths for several steps.
# each direction has same propability - making things easier
# you could simply create a list with an entry for each path of a tree diagram,
# as each path has the same probability
def add_one_step(steps: list):
random_step = [(1,0), (0,1), (-1,0), (0,-1)]
if steps == []:
return random_step
result = []
for i, steps_entry in enumerate(steps):
for step in random_step:
a = steps_entry[0]+step[0]
b = steps_entry[1]+step[1]
result.append((a, b))
return result
def get_multiple_steps(n=5):
final_directions = []
while n > 0:
final_directions = add_one_step(final_directions)
n -= 1
return final_directions
# be careful about how many steps to precalculate! The list lengths go with 4**n
precalculated_steps = []
for i in range(12):
precalculated_steps.append(get_multiple_steps(i))
You could use such precalculated values, to do several steps in one go.
n = 10
direction = precalculated_steps[n][np.random.randint(4**n)]
A += direction[0]
B += direction[1]
Thats the most simple aproach. If you want to go to higher n values, you have to think about how to reduce precalculation time (this is just a simple brute force calculation of all paths), and how to safe each result only once (and how many times it occurs).
The tricky part is your border and the stick. You have to chose a fitting matrix size depending on your current distance to border and stick.