Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visiting every cell in a grid in pseudorandom order without temporary storage

Tags:

algorithm

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)?

like image 588
Victor Liu Avatar asked Feb 27 '23 21:02

Victor Liu


1 Answers

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.

like image 75
Henk Holterman Avatar answered May 20 '23 09:05

Henk Holterman