A seeded random walk
A drunk ant that is perfectly repeatable, because you chose its dice.
Write walk(steps, seed). Return the path of a random walk on the integer grid as an array of steps + 1 points [x, y], starting at [0, 0]. Call show(points) after each point is added so the path draws as it wanders.
The walk uses its own dice, not Math.random, so every run is the same. Keep a state s that starts at seed. Before each step, update it with s = (s * 48271) % 2147483647, then pick a direction from s % 4: 0 moves right (x + 1), 1 moves down (y + 1), 2 moves left (x - 1), 3 moves up (y - 1). Here y grows downward, like the screen.
Example: walk(3, 1) updates s to 48271 (48271 % 4 is 3, up), then 182605794 (2, left), then 1291394886 (2, left). It returns [[0, 0], [0, -1], [-1, -1], [-2, -1]].
Tests you can see
walk(3, 1)[[0,0],[0,-1],[-1,-1],[-2,-1]]walk(40, 2026)[[0,0],[-1,0],[0,0],[-1,0],[0,0],[0,-1],[-1,-1],[-1,0],[0,0],[-1,0],[0,0],[0,-1],[0,0],[0,1],[-1,1],[-1,2],[-2,2],[-3,2],[-2,2],[-2,3],[-1,3],[-1,4],[-1,5],[-2,5],[-1,5],[0,5],[1,5],[1,6],[0,6],[0,5],[0,6],[-1,6],[0,6],[-1,6],[-1,5],[-1,6],[-2,6],[-1,6],[0,6],[-1,6],[-1,5]]
More tests are hidden. The server runs them all before a time counts.
Call show() while your code works and the run plays back here.
Tab indents · Cmd/Ctrl+Enter runs