Home › Blog › Shuffle and Randomize Arrays in ActionScript and JavaScript Shuffle and Randomize Arrays in ActionScript and JavaScript Sunday, July 24th , 2011. 0 comments Author: Steven Vachon
I’d been looking around for the best solution for this and most of what I had found either performed poorly or wasn’t quite random enough. Not so strangely in the end, the best solution was the most basic.
Note: This also works fine with the ActionScript 3 Vector()
class.
For ActionScript:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function shuffleArray( array :Array ) :Array
{
for ( var i:int =0 , len:int =array .length ; i< len; i++)
{
var j:int = Math .round ( Math .random ( ) * ( len-1 ) ) ;
var value:* = array [ i] ;
array [ i] = array [ j] ;
array [ j] = value;
}
return array ;
}
function shuffleArray(array:Array):Array { for (var i:int=0, len:int=array.length; i<len; i++) { var j:int = Math.round( Math.random() * (len-1) ); var value:* = array[i]; array[i] = array[j]; array[j] = value; } return array; }
For JavaScript:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function shuffleArray( array)
{
for ( var i= 0 , len= array.length ; i< len; i++ )
{
var j = Math .round ( Math .random ( ) * ( len- 1 ) ) ;
var value = array[ i] ;
array[ i] = array[ j] ;
array[ j] = value;
}
return array;
}
function shuffleArray(array) { for (var i=0, len=array.length; i<len; i++) { var j = Math.round( Math.random() * (len-1) ); var value = array[i]; array[i] = array[j]; array[j] = value; } return array; }
About This Article This post was filed by Steven Vachon under Engineering , Websites with the tags actionscript , adobe , browsers , flash , how-to , javascript , optimization , performance , tips , tricks and last modified on Dec-08-2014@10:50am .
Absolute-to-Relative URLs Single-File Facebook OAuth 2.0 Example
Comments
Top