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:
@@ -43,14 +43,20 @@ public class ArrayUtil
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Randomly shuffle the elements in the specified array.
|
* 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
|
// starting from the end of the list, repeatedly swap the element in
|
||||||
// question with a random element previous to it up to and including
|
// question with a random element previous to it up to and including
|
||||||
// itself
|
// itself
|
||||||
for (var ii :int = arr.length - 1; ii > 0; ii--) {
|
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];
|
var tmp :Object = arr[idx];
|
||||||
arr[idx] = arr[ii];
|
arr[idx] = arr[ii];
|
||||||
arr[ii] = tmp;
|
arr[ii] = tmp;
|
||||||
|
|||||||
Reference in New Issue
Block a user