Over a million developers have joined DZone.

Array Shuffle //JavaScript Function

·


[UPDATED CODE AND HELP CAN BE FOUND HERE]


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;
};
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}