diff --git a/lib/random/random.js b/lib/random/random.js index 63a1f61..eb4b901 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -180,18 +180,29 @@ class random { return newArray } + /** + * Select an array containing a subset of 'list' + * @param list + * @param limit + * @returns {Array} + */ static subset (list, limit) { if (!(Array.isArray(list))) { logger.traceback() throw new TypeError(`random.subset() received non-array type: (${list})`) } + if (typeof limit !== 'number') { limit = random.number(list.length + 1) } - let result = [] - for (let i = 0; i < limit; i++) { - result.push(random.pick(list)) + + // Deepclone list + const temp = JSON.parse(JSON.stringify(list)) + const result = [] + while (limit--) { + result.push(random.pop(temp)) } + return result }