Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Array Shuffle //JavaScript Function

DZone's Guide to

Array Shuffle //JavaScript Function

·
Free Resource


[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:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}