
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.
for (var i:uint = 0; i < myArray.length; i++) { var rand:uint = int(Math.random() * myArray.length); myArray.push( myArray.splice( rand, 1 )[0] ); }
Comments

You need to login to post a comment.
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] ); }
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] ); }
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 ); } }
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/
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
How about a different way of doing this - var len:int=myArray.length for (var i:int=0; i
Did that cut my comment off? var len:int=myArray.length; for (var i:int=0; i
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?
the 8 best rechargeable batteries for xbox one the8best.com