From: Jérôme Benoit Date: Mon, 1 Jan 2024 12:26:02 +0000 (+0100) Subject: refactor: remove home made random integer generator X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=1ccd07249dcfce8874ff9433ce4dfb4b597b1bd3;p=benchmarks-js.git refactor: remove home made random integer generator Signed-off-by: Jérôme Benoit --- diff --git a/benchmark-utils.mjs b/benchmark-utils.mjs index 7a681c1..1d486bf 100644 --- a/benchmark-utils.mjs +++ b/benchmark-utils.mjs @@ -1,4 +1,4 @@ -import { randomBytes, getRandomValues } from 'node:crypto' +import { randomBytes, getRandomValues, randomInt } from 'node:crypto' /** * Generate a cryptographically secure random number in the [0,1[ range @@ -16,23 +16,6 @@ export function secureRandomWithRandomValues () { return getRandomValues(new Uint32Array(1))[0] / 0x100000000 } -/** - * @param max - * @param min - * @returns - */ -export function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { - if (max < min || max < 0 || min < 0) { - throw new RangeError('Invalid interval') - } - max = Math.floor(max) - if (min != null && min !== 0) { - min = Math.ceil(min) - return Math.floor(secureRandom() * (max - min + 1)) + min - } - return Math.floor(secureRandom() * (max + 1)) -} - /** * * @param max @@ -80,7 +63,7 @@ export function generateRandomObject ( numberMax = Number.MAX_VALUE, numberGenerator = generateRandomFloat ) { - const size = generateRandomInteger(sizeMax) + const size = randomInt(sizeMax) const object = {} for (let i = 0; i < size; i++) { object[i.toString()] = numberGenerator(numberMax) diff --git a/quick-select.mjs b/quick-select.mjs index efbb9b7..d427026 100644 --- a/quick-select.mjs +++ b/quick-select.mjs @@ -1,5 +1,5 @@ +import { randomInt } from 'node:crypto' import Benchmark from 'benny' -import { generateRandomInteger } from './benchmark-utils.mjs' /** * @param numberOfWorkers @@ -12,7 +12,7 @@ function generateRandomTasksMap ( ) { const tasksArray = [] for (let i = 0; i < numberOfWorkers; i++) { - const task = [i, generateRandomInteger(maxNumberOfTasksPerWorker)] + const task = [i, randomInt(maxNumberOfTasksPerWorker)] tasksArray.push(task) } return new Map(tasksArray) @@ -63,7 +63,7 @@ const defaultPivotIndexSelect = (leftIndex, rightIndex) => { } const randomPivotIndexSelect = (leftIndex, rightIndex) => { - return generateRandomInteger(rightIndex, leftIndex) + return randomInt(leftIndex, rightIndex) } /**