/ Published in: JavaScript
URL: http://jsfromhell.com/array/shuffle
Scrambles the elements of an array. Created: 2005.11.03
Expand |
Embed | Plain Text
/************************************** * Jonas Raoni Soares Silva * http://www.joninhas.ath.cx **************************************/ shuffle = function(v){ //v1.0 for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); return v; };
Comments
Subscribe to comments
You need to login to post a comment.

You can use:
Array.prototype.shuffle = function() { for(var j, x, i = this.length; i; j = Math.floor(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x); }
With Math.floor() is more fast than parseInt()