Correct _.sample to avoid favoring putting the second half of the array into the front and the first half in the back.

This commit is contained in:
jdalton
2015-05-23 00:05:34 -07:00
parent 9ef5c5d639
commit 1276bf8c72

View File

@@ -7086,19 +7086,18 @@
var length = collection.length;
return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
}
var result = toArray(collection),
var index = -1,
result = toArray(collection),
length = result.length,
index = length;
lastIndex = length - 1;
n = nativeMin(n < 0 ? 0 : (+n || 0), length);
var end = length - n;
while (index-- > end) {
var rand = baseRandom(0, index),
othIndex = length - index - 1,
othValue = result[othIndex];
while (++index < n) {
var rand = baseRandom(index, lastIndex),
value = result[rand];
result[othIndex] = result[rand];
result[rand] = othValue;
result[rand] = result[index];
result[index] = value;
}
result.length = n;
return result;