Collections.shuffle().
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4441 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user