MCPcopy Index your code
hub / github.com/jashkenas/underscore / sample

Function sample

underscore-node-f.cjs:1561–1577  ·  view source on GitHub ↗
(obj, n, guard)

Source from the content-addressed store, hash-verified

1559// If **n** is not specified, returns a single random element.
1560// The internal `guard` argument allows it to work with `_.map`.
1561function sample(obj, n, guard) {
1562 if (n == null || guard) {
1563 if (!isArrayLike(obj)) obj = values(obj);
1564 return obj[random(obj.length - 1)];
1565 }
1566 var sample = toArray(obj);
1567 var length = getLength(sample);
1568 n = Math.max(Math.min(n, length), 0);
1569 var last = length - 1;
1570 for (var index = 0; index < n; index++) {
1571 var rand = random(index, last);
1572 var temp = sample[index];
1573 sample[index] = sample[rand];
1574 sample[rand] = temp;
1575 }
1576 return sample.slice(0, n);
1577}
1578
1579// Shuffle a collection.
1580function shuffle(obj) {

Callers 1

shuffleFunction · 0.70

Calls 3

valuesFunction · 0.70
randomFunction · 0.70
toArrayFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…