The procedure works like this: to get a number which is approximately uniform in , generate a (relatively long) sequence of “random” numbers in , and then compute their sum mod . According to this answer on the Mathematics StackExchange ( https://math.stackexchange.com/questions/338986/modulo-of-sum-of-random-variables ), the result of this process should approach the uniform distribution. From experimental evidence, this appears to be true.

This relies on the assumption that each number in the sequence is chosen independently of all others, which may not be true. Another drawback is that it could be easy to cheat if you’re good at tracking partial sums mod . For , this might be really easy for some people.

]]>In more detail, think of the slope of a line as given in the form (dr, dc), that is, the offset in terms of rows and columns from one crossed out number to the next. Then I only consider (1, -n mod p) and (-n^{-1} mod p, 1) and pick whichever one has the smaller absolute value for the non-unit dimension. There are certainly other ways it could be done — I like your idea of picking the closest. ]]>