Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


Array.shuffle

Flash Version: FLASH MX
Author: underaustin@ms15.url.com.tw
Total downloads: 14519
Category: Array
Publish Date: 2004-04-23 05:50:50

the code:
Array.prototype.shuffle=function(){
   for(i=0;i<this.length;i++){
      var tmp=this[i];
      var randomNum=random(this.length);
      this[i]=this[randomNum];
      this[randomNum]=tmp;
   }
}



// USAGE:

arr1=["a","b","c","d","e"];
arr1.shuffle();
trace(arr1);

Comments (9)

saintsvk (from Slovakia ) on 15th October 2006

When used 'i' before this scrip i get Flash 8 check syntax error.

So little correction here:
... for( > var < i

Array.prototype.shuffle=function(){
for( var i=0;i<this.length;i++){
var tmp=this[i];
var randomNum=random(this.length);
this[i]=this[randomNum];
this[randomNum]=tmp;
}
}

susmalucian (from ) on 10th May 2006

thanks sami@pyroid.com. i used your example to scramble a word :P .it works fine no problems

jim (from Canada ) on 16th March 2006

I like that last one. The only thing I would add is [0] at the end.

this.splice(Math.round((this.length-1)*Math.random()),1)[0]

This way you are pushing the value of the randomly chosen key into Arr2 and not the entire array yielded by splice. Otherwise you would be pushing arrays into Arr2

rod (from France ) on 4th March 2006

hi, the function of sami works fine, your's sephirot don't, for the reason simon said.

To me the most simple method is to use the one of the "hat' : remove randomly while there is something to remove.

//////////////////////////////////////////////////////////////////////////////////////////////
Array.prototype.shuffle=function(){
var Arr2 = new Array();
while(this.length>0){
Arr2.push(this.splice(Math.round((this.length-1)*Math.random()),1));
}
return Arr2;
}


var Arr1 = ["un","deux","trois","quatre","cinq","six","sept","huit","neuf","dix"];

arr2 = Arr1.shuffle();
//////////////////////////////////////////////////////////////////////////////////////////////

sami (from Finland ) on 13th October 2005

take it out random, put it back in random, do this for each index, demo included, enjoy ;)

maybe you could replace random with Math.random, but it seem random to me....

a = [1, 2, 3, 4, 5, 6, 7, 8];
Array.prototype.shuffle=function(){
var len=this.length;
for (l=0; l<len; l++) this.splice(random(this.length),0,this.splice(random(this.length), 1));
};
a.shuffle();
trace(a);

jenright on 17th June 2005

thanks guys - really got me out of a jam.....

simonwalter on 16th May 2005

Try this to avoid duplicates

function array_randomise($my_array:Array):Array{
var $return_array = Array();
var $original_length = $my_array.length;
for($i=0;$i<$original_length;$i++){
var $random_number = Math.floor(Math.random() * ($my_array.length));
$return_array[$i] = $my_array.splice($random_number,1);
}
return $return_array;
}

erivas on 10th August 2004

como estas

Brock.B.Henderson on 30th June 2004

There's one problem with this prototype--some of the indexes in the array could get deleted and you could end up with duplicates. I've had to do this before and to avoid that you can randomly slice indexes out of the array and push them into a new array and then exchange the two. Don't know if that'd work in a prototype function though. Guess you could pass them all back into the original array.