From 6f18912003de9b3412ee469862f826b2d4af6fef Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Wed, 1 Nov 2006 03:55:14 +0000 Subject: [PATCH] Collections.shuffle(). git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4441 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/Collections.as | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/as/com/threerings/util/Collections.as diff --git a/src/as/com/threerings/util/Collections.as b/src/as/com/threerings/util/Collections.as new file mode 100644 index 000000000..f17ddc5d6 --- /dev/null +++ b/src/as/com/threerings/util/Collections.as @@ -0,0 +1,25 @@ +package com.threerings.util { + +import mx.collections.ListCollectionView; + +/** + * Various collections-related utility methods. + */ +public class Collections +{ + /** + * Randomly shuffle the elements in the specified list. + */ + public static function shuffle (collection :ListCollectionView) :void + { + // starting from the end of the list, repeatedly swap the element + // in question with a random element previous to it up + // to and including itself + for (var ii :int = collection.length - 1; ii > 0; ii--) { + var idx :int = int(Math.random() * (ii + 1)) + var item :Object = collection.getItemAt(idx); + collection.setItemAt(collection.setItemAt(item, ii), idx); + } + } +} +}