Suppose you have an NxN grid, and you want to visit every cell exactly once, and in a pseudorandom order. The order doesn't have to be very random, but it does have to be seedable so that a different permutation of the grid cell ordering is possible. Is there a way to do this without simply generating a list of all cell coordinates, randomly permuting it, and indexing out of it? I mean, I want a formula (or rather, a general way of constructing such a formula) that can transform indices (i,j)
into (i',j')
in a one-to-one, fully-covering way.
Followup (actually intended) question: Can the same be done for an NxN grid where we only want to visit those elements for which i>j
(the lower triangle)?
Since each number N in the range 0..ROWS*COLS-1 deterministically points to a cell (N DIV ROWS, N MOD ROWS) this question is equivalent to this SO question. It has some useful answers and references.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With