Posted By

Activetuts on 01/13/11


Tagged


Versions (?)

Who likes this?

28 people have marked this snippet as a favorite

nyong15
kojok
chus
gandreini
elbjoern
EderChrono
toxin20
alsodenn
jasongrandelli
leadingmannigel
liakwee
ichnoweb
kure69
AndrewPotter
shuchii
JaaQ
im3der
souellette
elderhsouza
JoF
makeyko
jbyerson
thoaionline
coutlasssupreme
ringo380
carlokohan
dennywalker
flashdeveloper


Randomize an Array


 / Published in: ActionScript 3
 

URL: http://enva.to/e4ig6z

To randomize an array we loop over the length of the array, removing an object chosen at random and then adding it back onto the Array's end position. Think of this as having a deck of cards where you pick a card at random from the deck and move it to the top of the stack repeated for the total number of cards in the deck. It's important to note that the splice method returns an Array containing the removed object and not the object itself hence adding the [0] after the splice call to reference the contained object.

  1. for (var i:uint = 0; i < myArray.length; i++)
  2. {
  3. var rand:uint = int(Math.random() * myArray.length);
  4. myArray.push( myArray.splice( rand, 1 )[0] );
  5. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: mgeduld on January 19, 2011

This is useful. I would optimize it by caching the length. It's possible that arrays do this anyway, but I think there may be some overhead if you repeatedly call array.length. So...

var len : int = myArray.length;

for (var i:uint = 0; i < len; i++) { var rand:uint = int(Math.random() * len); myArray.push( myArray.splice( rand, 1 )[0] ); }

Posted By: mgeduld on January 19, 2011

This is useful. I would optimize it by caching the length. It's possible that arrays do this anyway, but I think there may be some overhead if you repeatedly call array.length. So...

var len : int = myArray.length;

for (var i:uint = 0; i < len; i++) { var rand:uint = int(Math.random() * len); myArray.push( myArray.splice( rand, 1 )[0] ); }

Posted By: viaria on January 23, 2011

this is cool, i make something it has the same purpose but by my way, i didnt use splice, good to know this now. i paste that long statement,

while (myArray.length < howManyNumber) { isEqual = false; newNumber = Math.ceil(Math.random() * len); for (var i:int = 0; i < myArray.length; i++) { if (myArray[i] == newNumber ) { isEqual = true; break; } } if (isEqual == false) { myArray.push(newNumber ); } }

Posted By: chrisaiv on April 7, 2011

The Casa Library (http://casalib.org) has an incredible set of Array utilities. I use this thing all the time. http://flashspeaksactionscript.com/casa-lib-arrayutil-sorting-searching-arrays/

Posted By: divillysausages on January 2, 2012

This is not the best way to do it. As previously mentioned, caching myArray.length will make it go quicker. Also, Array.splice() creates a new Array every time you call it (plus you have the added overhead of if you splice() at index 0, it needs to traverse the entire array to shift all the elements into place).

Much easier: var len:int = myArray.length; for ( var i:int = 0; i < len; i++ ) { // choose a random location and hold what's there var ran:int = int( Math.random() * len ); var old:* = myArray[ran]; // now swap them myArray[ran] = myArray[i]; myArray[i] = old; }

No new arrays, much faster, and each object is guaranteed to get sorted once

Posted By: CamWiseOwl on June 19, 2012

How about a different way of doing this - var len:int=myArray.length for (var i:int=0; i

Posted By: CamWiseOwl on June 19, 2012

Did that cut my comment off? var len:int=myArray.length; for (var i:int=0; i

Posted By: PureAS3 on August 28, 2012

Mhh I am not convinced by your suggestions ..

the faster is "while". The "splice" decreases each time the array length.

I do not store the lenght, but just use while (Array.length != 0) .. And each pass, splice and push in new array, no?

You need to login to post a comment.