DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Array Shuffle //JavaScript Function

11.03.2005
| 84524 views |
  • submit to reddit
        <a href="http://jsfromhell.com/array/shuffle">

[UPDATED CODE AND HELP CAN BE FOUND HERE]
</a>

usage

alert(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));

code based on Fisher-Yates (never heard about him hahaha) algorithm

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};