In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

flash on 2011-2-11

Array shuffle oneliner.
Get Adobe Flash player
by makc3d 11 Feb 2011
  • Related works: 1
  • Talk

    wh0 at 12 Feb 2011 08:58
    Here's an article about Microsoft's use of this kind of shuffling. http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html
    9re at 12 Feb 2011 15:49
    the above link mentions the defeats of this algorithm. i can give you a simple example. from the sequence 1, 2, 3 with an arbitrary natural number n (i include zero) [1, 2, 3] requires 2n times swapping of it's elements [2, 1, 3], [1, 3, 2], [3, 2, 1] requires 2n + 1 times swapping [2, 3, 1], [3, 1, 2] requires 2(n + 1) times swapping (you may know this as 'odd permutation' or 'even permutation') and not only 'parity' of the permutation but also the algorithm inside Array.sort does have effects on the distribution i did a distribution test on Fisher-Yates algorithm and Array.sort with random comparing function. http://wonderfl.net/c/5NW
    makc3d at 12 Feb 2011 16:36
    sure, non-randomness due to sort() implementation is (or should be:) expected. @9re, /c/5NW is not a valid url (needs one more character)
    makc3d at 12 Feb 2011 16:40
    oh, it's http://wonderfl.net/c/5NWR
    9re at 12 Feb 2011 17:02
    sorry. that's it. http://wonderfl.net/c/5NWR
    Embed
package  {
	import com.actionscriptbible.Example;
	/**
	 * Array shuffle oneliner.
	 */
	public class ShuffleArray extends Example {
		public function ShuffleArray () {
			var a:Array = [];
			for (var i:int = 0; i < 100; i++) a [i] = i;
			a.sort (function (a:*, b:*):Number { return (Math.random () > 0.5) ? 1 : -1; });
			trace (a);
		}
	}
}