I'm trying to perform explore a grid in a random fashion.
The five possible directions I can go are north, east, south, west, and stay, all of which are equally likely, at least initially.
The trouble with simply choosing a random direction from the above is that it ends up keeping my agent too close to the center (two random directions can cancel each other out very easily), which completely defeats the purpose of the random walk.
What I would like to do is to generate random directions in a random fashion that is unbiased as a whole, but which is more likely to choose a direction close to the the previously chosen direction.
Put another way, I need my RNG to have some kind of "momentum".
I came up with this algorithm:
def RandomWithMomentum(n, momentum=0.5):
from random import uniform
v = uniform(-1, 1)
for i in range(n):
yield v
v = v * momentum + uniform(-1, 1) * (1 - momentum)
which seems to give excellent results:
-0.04367186243339227
-0.1798381656787107
-0.07608795741137708
-0.0728742899528114
-0.06215075604982321
0.17952360050689026
0.016352984710556573
0.16954506853320414
0.3947467183848671
0.12785652121165636
... except that while this algorithm guarantees that positive and negative numbers are equally likely, it does not guarantee a uniform distribution in the interval from -1 to +1!
(This should be obvious if you realize that the numbers aren't necessarily bounded by -1 and +1!)
So my question is, how would I extend this (or some other algorithm) to choosing one of the five directions, instead of just a positive or negative number?