Quick “Shuffle” algorithm in JavaScript

I came up with a quick “shuffling” algorithm for JavaScript for a project I’m working on, and I’m just saving it here for future reference in case I need it again. Feel free to copy it if you need one too.

shuffle = function( list ) {
  var result = [];
  while( list.length > 0 ) {
    var i = Math.floor( list.length * Math.random() );
    result.push( list.splice( i, 1 ) );
  };
  return result;
}
DeliciousEmailShare

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>