MCPcopy Create free account
hub / github.com/dtmilano/AndroidViewClient / sample

Function sample

docs/_static/underscore-1.13.1.js:1520–1536  ·  view source on GitHub ↗
(obj, n, guard)

Source from the content-addressed store, hash-verified

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) {

Callers 1

shuffleFunction · 0.85

Calls 4

isArrayLikeFunction · 0.85
valuesFunction · 0.85
randomFunction · 0.85
cloneFunction · 0.85

Tested by

no test coverage detected