12
Context
In this challenge, a random walk is a stochastic process in which a particle at a position with integer coordinates (or a drunk man) moves at each integer time step (that is, at t = 1, 2, 3, ...
) by taking a step of size 1
to any of its neighbouring positions, with the direction of the step being aligned with the axis, but chosen randomly
For example, in one dimension, we may have the following table of positions at the given times, starting at t = 0
with the initial condition p = 2
:
----------
| t | p |
----------
| 0 | 2 |
----------
| 1 | 3 |
----------
| 2 | 2 |
----------
| 3 | 3 |
----------
| 4 | 2 |
----------
| 5 | 1 |
----------
| 6 | 0 |
----------
| 7 | -1 |
----------
| 8 | 0 |
----------
| 9 | -1 |
----------
...
Task
Your task is to simulate a random walk in n
dimensions from a supplied starting point, until the drunk man arrives at the origin for the first time, i.e. when we reach the point with coordinates [0,0,...,0]
for the first time. If we start at the origin, nothing has to be done because we already arrived.
In the example above, the program would have stopped at t = 6
.
You can take whatever probability distribution you want over the possible directions to go, so long as, for any position and any direction, there is a non-zero probability of that direction being picked.
Notice that your program must work for an arbitrary dimension, even though in practice, for higher dimensions it will not stop in a reasonable amount of time.
Input
Any initial position with integer coordinates, of any positive size (so 1 or more). You can infer the dimensions of the random walk from the size of the input or you can take the dimension as an extra input. If you do, assume the dimension given matches the size of the initial position.
Input of initial position can be in any sensible format, including:
- a list of integers
- one input coordinate per function argument
Your program should work for any n
, even though in practice it will time out for n >= 3
unless you are incredibly lucky :)
Output
You should return or print any of the following:
- all the intermediate positions occupied by the particle until the end of the random walk
- all the intermediate steps taken by the particle until the end of the random walk
The intermediate positions can be displayed in any human-readable way, as long as no two different positions are displayed the same way. Same applies to intermediate steps taken.
Test cases
These should finish on TIO often:
[3]
[1, 0]
[0, -2]
[1, 1]
[-1,1]
[0]
[0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
These will probably timeout, but that is fine as long as your program runs:
[1, 0, 0, 0]
[0, 0, 3, 0, -2, 100]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
You can check this reference program where I set the seed so that you can see it terminating for a random walk of 3 dimensions.
This is code-golf so shortest submission in bytes, wins! If you liked this challenge, consider upvoting it... And happy golfing!
Does that walk actually have to be random, or can we walk home deterministically? – xnor – 2020-02-22T12:27:57.900
And what exactly is the rule about stopping when you get to the origin? Can you keep going? Or pass it once but return and stop later? Does the stopping always have to happen in any run, or is this just part of the non-zero probability of arriving home? – xnor – 2020-02-22T12:34:00.277
@xnor I had used bad wording for that. I want non-zero probability of any direction being picked at any position, so we can't walk deterministically. You can use heuristics to weigh the chances, though. If you want that. And the stopping rule is the termination criteria for your program. Your program should stop as soon as you get home and only when you get home. – RGS – 2020-02-22T12:45:11.567
1The point here really isn't about getting home. It is about simulating a random walk. "Getting home" is just some artificial criterion for your programs to be able to stop at some point and provide some observable output. – RGS – 2020-02-22T12:49:32.777
Thanks, that clears it up. To make sure, any distribution that gives nonzero chance to each direction at each point is valid then? – xnor – 2020-02-22T12:50:49.447
@xnor yes, that is correct. In other words, given any two points, there is a non-zero probability of going from one to the other. Correct? – RGS – 2020-02-22T13:02:48.613
4For more than 2 dimensions there’s a nonzero probability that the walk never returns to the origin. So if the program outputs the steps when finished, it may never finish, and so no output will be produced. Is that allowed? Or should each step be output on the fly, producing a possibly infinite sequence of steps? – Luis Mendo – 2020-02-22T13:18:33.790
@LuisMendo It is ok that the program never finishes in those cases. You can output the steps on the fly or by the end of the program, that is up to you. I just want the programs to be able to handle arbitrary dimensions, I don't want to be maths :) – RGS – 2020-02-22T13:26:24.900
Included a reference program. Also, the RGS only starts on Monday, the 24th of February :D
– RGS – 2020-02-22T14:49:48.550Are we allowed to print 0,0 or the origin? – S.S. Anne – 2020-02-22T15:34:06.290
@S.S.Anne yes that is fine – RGS – 2020-02-22T15:40:47.463
1Because the probability of the directions doesn't have to be equal, I think a program that chose the dimension with the greatest absolute value with 90% probability, and chose to head toward the origin along that dimension with 90% probability, would comply and would reach the origin pretty quickly even for a high number of dimensions. – David Conrad – 2020-02-22T17:48:12.480
That's not a criticism, just an observation. Oh, and if two or more dimensions had equally high values it could pick between them with equal probability. – David Conrad – 2020-02-22T17:49:44.380
@DavidConrad that is correct, yes. But the point of the challenge is not to reach the origin, so I don't really mind if your program never really gets there :) – RGS – 2020-02-22T18:21:50.100
1I realize reaching the origin isn't the goal, but was thinking about it because of your comment that a program will probably time out for high n "unless you are incredibly lucky :)" – David Conrad – 2020-02-22T18:28:13.920
Must we output the beginning and ending positions as well, or can one (or both) of them be omitted? – JungHwan Min – 2020-02-22T22:22:45.010
@JungHwanMin one or both may be ommitted – RGS – 2020-02-22T22:48:00.607