forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrandomize.as
More file actions
32 lines (28 loc) · 764 Bytes
/
randomize.as
File metadata and controls
32 lines (28 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package utils.array
{
import utils.number.randomIntegerWithinRange;
/**
Creates new Array composed of passed Array's items in a random order.
@param inArray: Array to create copy of, and randomize.
@return A new Array composed of passed Array's items in a random order.
@example
<code>
var numberArray:Array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
trace(ArrayUtil.randomize(numberArray));
</code>
*/
public function randomize(inArray:Array):Array
{
var t:Array = new Array();
var r:Array = inArray.sort(
function(a:*, b:*):int
{
return randomIntegerWithinRange(0, 1) ? 1 : -1
}
, Array.RETURNINDEXEDARRAY);
var i:int = -1;
while (++i < inArray.length)
t.push(inArray[r[i]]);
return t;
}
}