|
|
@ -31,17 +31,22 @@ module.exports.prepareDb = function (filename, cb) { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Return an array with the numbers from 0 to n-1, in a random order |
|
|
|
* Return an array with the numbers from 0 to n-1, in a random order |
|
|
|
|
|
|
|
* Uses Fisher Yates algorithm |
|
|
|
* Useful to get fair tests |
|
|
|
* Useful to get fair tests |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function getRandomArray (n) { |
|
|
|
function getRandomArray (n) { |
|
|
|
var res, next; |
|
|
|
var res = [] |
|
|
|
|
|
|
|
, i, j, temp |
|
|
|
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
if (n === 0) { return []; } |
|
|
|
for (i = 0; i < n; i += 1) { res[i] = i; } |
|
|
|
if (n === 1) { return [0]; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res = getRandomArray(n - 1); |
|
|
|
for (i = n - 1; i >= 1; i -= 1) { |
|
|
|
next = Math.floor(Math.random() * n); |
|
|
|
j = Math.floor((i + 1) * Math.random()); |
|
|
|
res.splice(next, 0, n - 1); // Add n-1 at a random position in the array
|
|
|
|
temp = res[i]; |
|
|
|
|
|
|
|
res[i] = res[j]; |
|
|
|
|
|
|
|
res[j] = temp; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return res; |
|
|
|
return res; |
|
|
|
}; |
|
|
|
}; |
|
|
|