From 274811e8f61496d00c555e2fcbff0ae62c4d0352 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Thu, 6 Dec 2007 03:34:59 +0000 Subject: [PATCH] Accept an optional Random for shuffle(). git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4903 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/ArrayUtil.as | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/as/com/threerings/util/ArrayUtil.as b/src/as/com/threerings/util/ArrayUtil.as index 3b36141c2..8dfa5f029 100644 --- a/src/as/com/threerings/util/ArrayUtil.as +++ b/src/as/com/threerings/util/ArrayUtil.as @@ -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;