From b1ee4ac29c79b2d520fc1abeaca0bda9fcea5d17 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 2 May 2013 23:12:05 +0200 Subject: [PATCH] Can create random arrays for benchmarks --- lib/customUtils.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/customUtils.js b/lib/customUtils.js index b5f3057..1dfa06e 100644 --- a/lib/customUtils.js +++ b/lib/customUtils.js @@ -57,8 +57,26 @@ function deepCopy (obj) { } +/** + * Return an array with the numbers from 0 to n-1, in a random order + * Used in the benchmarks + */ +function getRandomArray (n) { + var res, next; + + if (n === 0) { return []; } + if (n === 1) { return [0]; } + + res = getRandomArray(n - 1); + next = Math.floor(Math.random() * n); + res.splice(next, 0, n - 1); // Add n-1 at a random position in the array + + return res; +} + module.exports.ensureDirectoryExists = ensureDirectoryExists; module.exports.uid = uid; module.exports.deepCopy = deepCopy; +module.exports.getRandomArray = getRandomArray;