(obj, n, guard)
| 1518 | // If **n** is not specified, returns a single random element. |
| 1519 | // The internal `guard` argument allows it to work with `_.map`. |
| 1520 | function sample(obj, n, guard) { |
| 1521 | if (n == null || guard) { |
| 1522 | if (!isArrayLike(obj)) obj = values(obj); |
| 1523 | return obj[random(obj.length - 1)]; |
| 1524 | } |
| 1525 | var sample = isArrayLike(obj) ? clone(obj) : values(obj); |
| 1526 | var length = getLength(sample); |
| 1527 | n = Math.max(Math.min(n, length), 0); |
| 1528 | var last = length - 1; |
| 1529 | for (var index = 0; index < n; index++) { |
| 1530 | var rand = random(index, last); |
| 1531 | var temp = sample[index]; |
| 1532 | sample[index] = sample[rand]; |
| 1533 | sample[rand] = temp; |
| 1534 | } |
| 1535 | return sample.slice(0, n); |
| 1536 | } |
| 1537 | |
| 1538 | // Shuffle a collection. |
| 1539 | function shuffle(obj) { |
no test coverage detected