Accept an optional Random for shuffle().

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4903 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2007-12-06 03:34:59 +00:00
parent fcdace5589
commit 274811e8f6
+8 -2
View File
@@ -43,14 +43,20 @@ public class ArrayUtil
/**
* Randomly shuffle the elements in the specified array.
*
* @param rando a random number generator to use, or null if you don't care.
*/
public static function shuffle (arr :Array) :void
public static function shuffle (arr :Array, rando :Random = null) :void
{
var randFunc :Function = (rando != null) ? rando.nextInt :
function (n :int) :int {
return int(Math.random() * n);
};
// 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 = arr.length - 1; ii > 0; ii--) {
var idx :int = int(Math.random() * (ii + 1));
var idx :int = randFunc(ii + 1);
var tmp :Object = arr[idx];
arr[idx] = arr[ii];
arr[ii] = tmp;