Implementing an efficient random iterator over a multi-level data structure
FRONT UPDATE: Ken B. has pointed out that I was thinking like a C programmer in writing this: I was thinking in terms of shuffling arrays of C-like integers versus arrays of larger C data structures. In that situation, my algorithm would be more efficient. But that is not how Python does things! These are Python lists, not C arrays, and the list is a list of pointers, whether the list is of integers or of agents. So the shuffle has the same cost either way! In other words, I did a bunch of fancy coding that would be nice in a different language, but that makes no difference in the one I am actually using. Still, the coding was interesting. FRONT UPDATE II: An important lesson I learn yet again -- I guess I am rustier than I thought! -- Don't "optimize" without profiling (timing portions of) your code! I timed my fancy-pants version of the random iteration, as well as of reverse iteration, against the "naive" versions that just used basic Python op...