Fix _.shuffle on small collections. [closes #609]

This commit is contained in:
John-David Dalton
2014-07-08 08:28:50 -07:00
parent 4ddc3b518b
commit b24b8315d3
2 changed files with 12 additions and 12 deletions

View File

@@ -5242,19 +5242,16 @@
function shuffle(collection) {
collection = toIterable(collection);
var index = 0,
var index = -1,
length = collection.length,
result = Array(length);
if (length) {
result[index] = collection[index];
}
while (++index < length) {
var value = collection[index],
rand = baseRandom(0, index - 1);
result[index] = result[rand];
result[rand] = value;
var rand = baseRandom(0, index);
if (index != rand) {
result[index] = result[rand];
}
result[rand] = collection[index];
}
return result;
}