// Shuffle a collection, using the modern version of the // [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle). // `shuffle` 函数。 _.shuffle = function(obj) { var set = isArrayLike(obj) ? obj : _.values(obj); var length = set.length; var shuffled = Array(length); for (var index = 0, rand; index < length; index++) { rand = _.random(0, index); if (rand !== index) shuffled[index] = shuffled[rand]; shuffled[rand] = set[index]; } return shuffled; };
每次循环从前面的 index 个元素中随机选择一个元素 shuffle[rand]。将这个元素与第 index 个元素进行交换,直到 index == length 为止。这样对元素进行随机交换,对于每个结果所获得概率是均匀的。_.shuffle 方法是返回一个新的乱序数组,所以需要一个新的数组来存储。
对原有数组进行乱序:
1 2 3 4 5 6 7 8 9 10 11
functionshuffle(arr) { var length = arr.length; for (var index = 0, rand; index < length; index ++) { rand = Math.floor(Math.random() * (length - 1));