Posted By

Leech on 07/21/06


Tagged

arrays


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

jkochis
nicolaspar
vali29
gfazioli


Shuffler v1.0


 / Published in: JavaScript
 

URL: http://jsfromhell.com/array/shuffle

Scrambles the elements of an array. Created: 2005.11.03

  1. /**************************************
  2. * Jonas Raoni Soares Silva
  3. * http://www.joninhas.ath.cx
  4. **************************************/
  5.  
  6. shuffle = function(v){ //v1.0
  7. for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
  8. return v;
  9. };

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: gfazioli on February 16, 2009

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()

You need to login to post a comment.