Shuffling Arrays

For some reason, there doesn’t seem to be any sane example of shuffling arrays in ActionScript blogland. I discovered this when I was trying to send a lazy link to someone of an implementation rather than write the code myself. Oh, by sane I mean vaguely efficient, not overusing crazy Math.floor or Array.slice functions.

Shuffling data isn’t a new problem, but people seem to always want to re- invent things. Alas, it’s often re-invented poorly. Here’s a nice vintage shuffling algorithm for you from 1964 (although the idea is based on a method from 1938. History rocks.). Example usage is in the huge header comment:

Feel free to extend the class to support typed Vectors. Sadly, you have to implement each Vector type yourself as AS3 currently has no non-hacky method of implementing template functions.