Over a million developers have joined DZone.

Generating A Shuffled Array.

DZone's Guide to

Generating A Shuffled Array.

Free Resource
Adds a method to Array to return a shuffled version of the array.

if (!Array.prototype.shuffle) {
    Array.prototype.shuffle = function() {
        // Clone this array.
        var result = this.concat();

        // Swap each element with another randomly selected one.
        for (var i = 0; i < result.length; i++) {
            var j = i;
            while (j == i) {
                j = Math.floor(Math.random() * result.length);
            var contents = result[i];
            result[i]    = arr[j];
            result[j]    = contents;

        return result;

Opinions expressed by DZone contributors are their own.


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.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}