晴明的博客园 GitHub      CodePen      CodeWars     

[js] Fisher-Yates

#

function shuffle(array) {
    var m = array.length,
        t, i;
    while (m) {
        i = Math.floor(Math.random() * m--);
        t = array[m];
        array[m] = array[i];
        array[i] = t;
    }
    return array;
}

 

#

function shuffle(array) {
    var copy = [],
        n = array.length,
        i;
    while (n) {
        i = Math.floor(Math.random() * n--);
        copy.push(array.splice(i, 1)[0]);
    }
    return copy;
}

 

#

function shuffle(array) {
    return array.sort(function() {
        return Math.random() - 0.5
    });
}

 

posted @ 2016-04-28 17:42  晴明桑  阅读(185)  评论(0)    收藏  举报