MCPcopy Index your code
hub / github.com/node-modules/utility / randomSlice

Function randomSlice

src/array.ts:4–17  ·  view source on GitHub ↗
(arr: T[], num?: number)

Source from the content-addressed store, hash-verified

2 * Array random slice with items count
3 */
4export function randomSlice<T = any>(arr: T[], num?: number): T[] {
5 if (!num || num >= arr.length) {
6 return arr.slice();
7 }
8 const index = Math.floor(Math.random() * arr.length);
9 const a: T[] = [];
10 for (let i = 0, j = index; i < num; i++) {
11 a.push(arr[j++]);
12 if (j === arr.length) {
13 j = 0;
14 }
15 }
16 return a;
17}
18
19/**
20 * Remove one exists element from an array

Callers 1

array.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…