Comparing One-Step vs. Two-Step Random Selection Methods in a Code Golf Challenge.

20 Views Asked by At

One-Step

I'm reviewing answers to this code golf challenge. Here's a target cell marked by X that was chosen using rand()*512 (rand() in C) ensuring that each cell has an equal chance ($\frac{1}{512}$) of being chosen for modification.

....................X...........
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................
................................

Two-Step

Another solution first picked a random row rand()*16, then a random cell within that row rand()*32.

Question

Does using that Two-Step method to pick the target cell violate the rule that each cell has an equal chance ($\frac{1}{512}$) of being chosen for modification, but perhaps it's okay if the following is true?

P(target cell) = P(row) $\times$ P(cell in row) = $\frac{1}{16} \times \frac{1}{32} = \frac{1}{512}$