From 1269195cca30a632d9cea1665ab639f0ee9571f1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 19 Nov 2024 16:08:21 +0100 Subject: [PATCH] chore(desp-dev): apply updates MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .lintstagedrc.js | 2 +- benchmark-utils.mjs | 32 ++++----- busy-wait.mjs | 40 +++++------ max.mjs | 8 +-- min.mjs | 8 +-- package.json | 6 +- pnpm-lock.yaml | 89 ++++++++++------------- quick-select.mjs | 170 ++++++++++++++++++++++---------------------- random.mjs | 24 +++---- 9 files changed, 183 insertions(+), 196 deletions(-) diff --git a/.lintstagedrc.js b/.lintstagedrc.js index 720c26a..8e1f8db 100644 --- a/.lintstagedrc.js +++ b/.lintstagedrc.js @@ -1,8 +1,8 @@ export default { + '*.json': ['biome format --no-errors-on-unmatched --write'], '*.{js,jsx,cjs,mjs}': [ 'biome format --no-errors-on-unmatched --write', 'eslint --cache --fix', ], '*.{md,yml,yaml}': ['prettier --cache --write'], - '*.json': ['biome format --no-errors-on-unmatched --write'], } diff --git a/benchmark-utils.mjs b/benchmark-utils.mjs index 8e32f90..02e2a95 100644 --- a/benchmark-utils.mjs +++ b/benchmark-utils.mjs @@ -1,21 +1,5 @@ import { getRandomValues, randomBytes, randomInt } from 'node:crypto' -/** - * Generate a cryptographically secure random number in the [0,1[ range - * @returns - */ -export function secureRandom () { - return randomBytes(4).readUInt32LE() / 0x100000000 -} - -/** - * Generate a cryptographically secure random number in the [0,1[ range - * @returns - */ -export function secureRandomWithRandomValues () { - return getRandomValues(new Uint32Array(1))[0] / 0x100000000 -} - /** * * @param max @@ -71,6 +55,22 @@ export function generateRandomObject ( return object } +/** + * Generate a cryptographically secure random number in the [0,1[ range + * @returns + */ +export function secureRandom () { + return randomBytes(4).readUInt32LE() / 0x100000000 +} + +/** + * Generate a cryptographically secure random number in the [0,1[ range + * @returns + */ +export function secureRandomWithRandomValues () { + return getRandomValues(new Uint32Array(1))[0] / 0x100000000 +} + /** * @param ms * @returns diff --git a/busy-wait.mjs b/busy-wait.mjs index dd9f691..3786e8c 100644 --- a/busy-wait.mjs +++ b/busy-wait.mjs @@ -5,26 +5,6 @@ import { sleep } from './benchmark-utils.mjs' const timeout = 2000 const interval = 1000 -/** - * @param timeoutMs - */ -function dummyTimeoutBusyWait (timeoutMs) { - const timeoutTimestampMs = performance.now() + timeoutMs - // eslint-disable-next-line no-empty - do {} while (performance.now() < timeoutTimestampMs) -} - -/** - * @param timeoutMs - * @param intervalMs - */ -async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) { - const timeoutTimestampMs = performance.now() + timeoutMs - do { - await sleep(intervalMs) - } while (performance.now() < timeoutTimestampMs) -} - /** * @param timeoutMs * @param intervalMs @@ -41,6 +21,15 @@ async function divideAndConquerTimeoutBusyWait ( } while (count <= tries) } +/** + * @param timeoutMs + */ +function dummyTimeoutBusyWait (timeoutMs) { + const timeoutTimestampMs = performance.now() + timeoutMs + // eslint-disable-next-line no-empty + do {} while (performance.now() < timeoutTimestampMs) +} + /** * @param timeoutMs * @param intervalMs @@ -59,6 +48,17 @@ async function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) { }) } +/** + * @param timeoutMs + * @param intervalMs + */ +async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) { + const timeoutTimestampMs = performance.now() + timeoutMs + do { + await sleep(intervalMs) + } while (performance.now() < timeoutTimestampMs) +} + group('Busy wait', () => { bench('dummyTimeoutBusyWait', () => { dummyTimeoutBusyWait(timeout) diff --git a/max.mjs b/max.mjs index 12b458c..23d856e 100644 --- a/max.mjs +++ b/max.mjs @@ -23,9 +23,9 @@ function loopMax (values) { * @param values * @returns */ -function reduceTernaryMax (values) { +function reduceMathMax (values) { return values.reduce( - (maximum, num) => (maximum > num ? maximum : num), + (maximum, num) => Math.max(maximum, num), Number.NEGATIVE_INFINITY ) } @@ -35,9 +35,9 @@ function reduceTernaryMax (values) { * @param values * @returns */ -function reduceMathMax (values) { +function reduceTernaryMax (values) { return values.reduce( - (maximum, num) => Math.max(maximum, num), + (maximum, num) => (maximum > num ? maximum : num), Number.NEGATIVE_INFINITY ) } diff --git a/min.mjs b/min.mjs index 44beb09..8d259fe 100644 --- a/min.mjs +++ b/min.mjs @@ -23,9 +23,9 @@ function loopMin (values) { * @param values * @returns */ -function reduceTernaryMin (values) { +function reduceMathMin (values) { return values.reduce( - (minimum, num) => (minimum < num ? minimum : num), + (minimum, num) => Math.min(minimum, num), Number.POSITIVE_INFINITY ) } @@ -35,9 +35,9 @@ function reduceTernaryMin (values) { * @param values * @returns */ -function reduceMathMin (values) { +function reduceTernaryMin (values) { return values.reduce( - (minimum, num) => Math.min(minimum, num), + (minimum, num) => (minimum < num ? minimum : num), Number.POSITIVE_INFINITY ) } diff --git a/package.json b/package.json index 993f944..a7a8aa8 100644 --- a/package.json +++ b/package.json @@ -75,13 +75,13 @@ }, "devDependencies": { "@biomejs/biome": "^1.9.4", - "@commitlint/cli": "^19.5.0", - "@commitlint/config-conventional": "^19.5.0", + "@commitlint/cli": "^19.6.0", + "@commitlint/config-conventional": "^19.6.0", "@eslint/js": "^9.15.0", "eslint": "^9.15.0", "eslint-define-config": "^2.1.0", "eslint-plugin-jsdoc": "^50.5.0", - "eslint-plugin-perfectionist": "^3.9.1", + "eslint-plugin-perfectionist": "^4.0.1", "husky": "^9.1.7", "lint-staged": "^15.2.10", "neostandard": "^0.11.8", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fb1dc6e..4d99626 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -46,11 +46,11 @@ importers: specifier: ^1.9.4 version: 1.9.4 '@commitlint/cli': - specifier: ^19.5.0 - version: 19.5.0(@types/node@22.9.0)(typescript@5.5.4) + specifier: ^19.6.0 + version: 19.6.0(@types/node@22.9.0)(typescript@5.5.4) '@commitlint/config-conventional': - specifier: ^19.5.0 - version: 19.5.0 + specifier: ^19.6.0 + version: 19.6.0 '@eslint/js': specifier: ^9.15.0 version: 9.15.0 @@ -64,8 +64,8 @@ importers: specifier: ^50.5.0 version: 50.5.0(eslint@9.15.0(jiti@1.21.6)) eslint-plugin-perfectionist: - specifier: ^3.9.1 - version: 3.9.1(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4) + specifier: ^4.0.1 + version: 4.0.1(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4) husky: specifier: ^9.1.7 version: 9.1.7 @@ -142,13 +142,13 @@ packages: cpu: [x64] os: [win32] - '@commitlint/cli@19.5.0': - resolution: {integrity: sha512-gaGqSliGwB86MDmAAKAtV9SV1SHdmN8pnGq4EJU4+hLisQ7IFfx4jvU4s+pk6tl0+9bv6yT+CaZkufOinkSJIQ==} + '@commitlint/cli@19.6.0': + resolution: {integrity: sha512-v17BgGD9w5KnthaKxXnEg6KLq6DYiAxyiN44TpiRtqyW8NSq+Kx99mkEG8Qo6uu6cI5eMzMojW2muJxjmPnF8w==} engines: {node: '>=v18'} hasBin: true - '@commitlint/config-conventional@19.5.0': - resolution: {integrity: sha512-OBhdtJyHNPryZKg0fFpZNOBM1ZDbntMvqMuSmpfyP86XSfwzGw4CaoYRG4RutUPg0BTK07VMRIkNJT6wi2zthg==} + '@commitlint/config-conventional@19.6.0': + resolution: {integrity: sha512-DJT40iMnTYtBtUfw9ApbsLZFke1zKh6llITVJ+x9mtpHD08gsNXaIRqHTmwTZL3dNX5+WoyK7pCN/5zswvkBCQ==} engines: {node: '>=v18'} '@commitlint/config-validator@19.5.0': @@ -167,12 +167,12 @@ packages: resolution: {integrity: sha512-yNy088miE52stCI3dhG/vvxFo9e4jFkU1Mj3xECfzp/bIS/JUay4491huAlVcffOoMK1cd296q0W92NlER6r3A==} engines: {node: '>=v18'} - '@commitlint/is-ignored@19.5.0': - resolution: {integrity: sha512-0XQ7Llsf9iL/ANtwyZ6G0NGp5Y3EQ8eDQSxv/SRcfJ0awlBY4tHFAvwWbw66FVUaWICH7iE5en+FD9TQsokZ5w==} + '@commitlint/is-ignored@19.6.0': + resolution: {integrity: sha512-Ov6iBgxJQFR9koOupDPHvcHU9keFupDgtB3lObdEZDroiG4jj1rzky60fbQozFKVYRTUdrBGICHG0YVmRuAJmw==} engines: {node: '>=v18'} - '@commitlint/lint@19.5.0': - resolution: {integrity: sha512-cAAQwJcRtiBxQWO0eprrAbOurtJz8U6MgYqLz+p9kLElirzSCc0vGMcyCaA1O7AqBuxo11l1XsY3FhOFowLAAg==} + '@commitlint/lint@19.6.0': + resolution: {integrity: sha512-LRo7zDkXtcIrpco9RnfhOKeg8PAnE3oDDoalnrVU/EVaKHYBWYL1DlRR7+3AWn0JiBqD8yKOfetVxJGdEtZ0tg==} engines: {node: '>=v18'} '@commitlint/load@19.5.0': @@ -195,8 +195,8 @@ packages: resolution: {integrity: sha512-CU/GscZhCUsJwcKTJS9Ndh3AKGZTNFIOoQB2n8CmFnizE0VnEuJoum+COW+C1lNABEeqk6ssfc1Kkalm4bDklA==} engines: {node: '>=v18'} - '@commitlint/rules@19.5.0': - resolution: {integrity: sha512-hDW5TPyf/h1/EufSHEKSp6Hs+YVsDMHazfJ2azIk9tHPXS6UqSz1dIRs1gpqS3eMXgtkT7JH6TW4IShdqOwhAw==} + '@commitlint/rules@19.6.0': + resolution: {integrity: sha512-1f2reW7lbrI0X0ozZMesS/WZxgPa4/wi56vFuJENBmed6mWq5KsheN/nxqnl/C23ioxpPO/PL6tXpiiFy5Bhjw==} engines: {node: '>=v18'} '@commitlint/to-lines@19.5.0': @@ -288,8 +288,8 @@ packages: resolution: {integrity: sha512-cq8o4cWH0ibXh9VGi5P20Tu9XF/0fFXl9EUinr9QfTM7a7p0oTA4iJRCQWppXR1Pg8dSM0UCItCkPwsk9qWWYA==} engines: {node: ^12.20.0 || ^14.18.0 || >=16.0.0} - '@stylistic/eslint-plugin@2.10.1': - resolution: {integrity: sha512-U+4yzNXElTf9q0kEfnloI9XbOyD4cnEQCxjUI94q0+W++0GAEQvJ/slwEj9lwjDHfGADRSr+Tco/z0XJvmDfCQ==} + '@stylistic/eslint-plugin@2.11.0': + resolution: {integrity: sha512-PNRHbydNG5EH8NK4c+izdJlxajIR6GxcUhzsYNRsn6Myep4dsZt0qFCz3rCPnkvgO5FYibDcMqgNHUT+zvjYZw==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: '>=8.40.0' @@ -695,24 +695,11 @@ packages: peerDependencies: eslint: '>=8.23.0' - eslint-plugin-perfectionist@3.9.1: - resolution: {integrity: sha512-9WRzf6XaAxF4Oi5t/3TqKP5zUjERhasHmLFHin2Yw6ZAp/EP/EVA2dr3BhQrrHWCm5SzTMZf0FcjDnBkO2xFkA==} + eslint-plugin-perfectionist@4.0.1: + resolution: {integrity: sha512-D7AmBw98rdfqf9wR3JTF1eLZpKmmG4jXRUSPzl2s5vVzUVSLz8pJx0RhWN6x/aHVjcJkeFBe/P4SCQWM+PdKkA==} engines: {node: ^18.0.0 || >=20.0.0} peerDependencies: - astro-eslint-parser: ^1.0.2 eslint: '>=8.0.0' - svelte: '>=3.0.0' - svelte-eslint-parser: ^0.41.1 - vue-eslint-parser: '>=9.0.0' - peerDependenciesMeta: - astro-eslint-parser: - optional: true - svelte: - optional: true - svelte-eslint-parser: - optional: true - vue-eslint-parser: - optional: true eslint-plugin-promise@7.1.0: resolution: {integrity: sha512-8trNmPxdAy3W620WKDpaS65NlM5yAumod6XeC4LOb+jxlkG4IVcp68c6dXY2ev+uT4U1PtG57YDV6EGAXN0GbQ==} @@ -1241,12 +1228,13 @@ packages: ms@2.1.3: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} - natural-compare-lite@1.4.0: - resolution: {integrity: sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==} - natural-compare@1.4.0: resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==} + natural-orderby@5.0.0: + resolution: {integrity: sha512-kKHJhxwpR/Okycz4HhQKKlhWe4ASEfPgkSWNmKFHd7+ezuQlxkA5cM3+XkBPvm1gmHen3w53qsYAv+8GwRrBlg==} + engines: {node: '>=18'} + neostandard@0.11.8: resolution: {integrity: sha512-gKgvK0EDlA0x7F1hMu/UdPCRtSSG4d9CBxVym4PMWoHz8+a1ffEz2fPU5FMH3RUqzL1ACvroUMiywMGWZSY+Mw==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} @@ -1761,10 +1749,10 @@ snapshots: '@biomejs/cli-win32-x64@1.9.4': optional: true - '@commitlint/cli@19.5.0(@types/node@22.9.0)(typescript@5.5.4)': + '@commitlint/cli@19.6.0(@types/node@22.9.0)(typescript@5.5.4)': dependencies: '@commitlint/format': 19.5.0 - '@commitlint/lint': 19.5.0 + '@commitlint/lint': 19.6.0 '@commitlint/load': 19.5.0(@types/node@22.9.0)(typescript@5.5.4) '@commitlint/read': 19.5.0 '@commitlint/types': 19.5.0 @@ -1774,7 +1762,7 @@ snapshots: - '@types/node' - typescript - '@commitlint/config-conventional@19.5.0': + '@commitlint/config-conventional@19.6.0': dependencies: '@commitlint/types': 19.5.0 conventional-changelog-conventionalcommits: 7.0.2 @@ -1800,16 +1788,16 @@ snapshots: '@commitlint/types': 19.5.0 chalk: 5.3.0 - '@commitlint/is-ignored@19.5.0': + '@commitlint/is-ignored@19.6.0': dependencies: '@commitlint/types': 19.5.0 semver: 7.6.3 - '@commitlint/lint@19.5.0': + '@commitlint/lint@19.6.0': dependencies: - '@commitlint/is-ignored': 19.5.0 + '@commitlint/is-ignored': 19.6.0 '@commitlint/parse': 19.5.0 - '@commitlint/rules': 19.5.0 + '@commitlint/rules': 19.6.0 '@commitlint/types': 19.5.0 '@commitlint/load@19.5.0(@types/node@22.9.0)(typescript@5.5.4)': @@ -1853,7 +1841,7 @@ snapshots: lodash.mergewith: 4.6.2 resolve-from: 5.0.0 - '@commitlint/rules@19.5.0': + '@commitlint/rules@19.6.0': dependencies: '@commitlint/ensure': 19.5.0 '@commitlint/message': 19.5.0 @@ -1945,7 +1933,7 @@ snapshots: '@pkgr/core@0.1.1': {} - '@stylistic/eslint-plugin@2.10.1(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4)': + '@stylistic/eslint-plugin@2.11.0(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4)': dependencies: '@typescript-eslint/utils': 8.15.0(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4) eslint: 9.15.0(jiti@1.21.6) @@ -2470,13 +2458,12 @@ snapshots: minimatch: 9.0.5 semver: 7.6.3 - eslint-plugin-perfectionist@3.9.1(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4): + eslint-plugin-perfectionist@4.0.1(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4): dependencies: '@typescript-eslint/types': 8.15.0 '@typescript-eslint/utils': 8.15.0(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4) eslint: 9.15.0(jiti@1.21.6) - minimatch: 9.0.5 - natural-compare-lite: 1.4.0 + natural-orderby: 5.0.0 transitivePeerDependencies: - supports-color - typescript @@ -3018,14 +3005,14 @@ snapshots: ms@2.1.3: {} - natural-compare-lite@1.4.0: {} - natural-compare@1.4.0: {} + natural-orderby@5.0.0: {} + neostandard@0.11.8(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4): dependencies: '@humanwhocodes/gitignore-to-minimatch': 1.0.2 - '@stylistic/eslint-plugin': 2.10.1(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4) + '@stylistic/eslint-plugin': 2.11.0(eslint@9.15.0(jiti@1.21.6))(typescript@5.5.4) eslint: 9.15.0(jiti@1.21.6) eslint-plugin-n: 17.13.2(eslint@9.15.0(jiti@1.21.6)) eslint-plugin-promise: 7.1.0(eslint@9.15.0(jiti@1.21.6)) diff --git a/quick-select.mjs b/quick-select.mjs index 411c897..b158c6b 100644 --- a/quick-select.mjs +++ b/quick-select.mjs @@ -20,6 +20,22 @@ function generateRandomTasksMap ( const tasksMap = generateRandomTasksMap(60, 20) +/** + * @param tasksMap + * @returns + */ +function arraySortSelect (tasksMap) { + const tasksArray = Array.from(tasksMap) + return tasksArray.sort((a, b) => { + if (a[1] < b[1]) { + return -1 + } else if (a[1] > b[1]) { + return 1 + } + return 0 + })[0] +} + /** * @param tasksMap * @returns @@ -38,22 +54,6 @@ function loopSelect (tasksMap) { return [minKey, minValue] } -/** - * @param tasksMap - * @returns - */ -function arraySortSelect (tasksMap) { - const tasksArray = Array.from(tasksMap) - return tasksArray.sort((a, b) => { - if (a[1] < b[1]) { - return -1 - } else if (a[1] > b[1]) { - return 1 - } - return 0 - })[0] -} - const defaultComparator = (a, b) => { return a < b } @@ -66,17 +66,6 @@ const randomPivotIndexSelect = (leftIndex, rightIndex) => { return randomInt(leftIndex, rightIndex) } -/** - * @param array - * @param index1 - * @param index2 - */ -function swap (array, index1, index2) { - const tmp = array[index1] - array[index1] = array[index2] - array[index2] = tmp -} - /** * @param array * @param leftIndex @@ -105,6 +94,68 @@ function partition ( return storeIndex } +/** + * @param tasksMap + * @returns + */ +function quickSelectLoop (tasksMap) { + const tasksArray = Array.from(tasksMap) + + return selectLoop(tasksArray, 0, 0, tasksArray.length - 1, (a, b) => { + return a[1] < b[1] + }) +} + +/** + * @param tasksMap + * @returns + */ +function quickSelectLoopRandomPivot (tasksMap) { + const tasksArray = Array.from(tasksMap) + + return selectLoop( + tasksArray, + 0, + 0, + tasksArray.length - 1, + (a, b) => { + return a[1] < b[1] + }, + randomPivotIndexSelect + ) +} + +/** + * @param tasksMap + * @returns + */ +function quickSelectRecursion (tasksMap) { + const tasksArray = Array.from(tasksMap) + + return selectRecursion(tasksArray, 0, 0, tasksArray.length - 1, (a, b) => { + return a[1] < b[1] + }) +} + +/** + * @param tasksMap + * @returns + */ +function quickSelectRecursionRandomPivot (tasksMap) { + const tasksArray = Array.from(tasksMap) + + return selectRecursion( + tasksArray, + 0, + 0, + tasksArray.length - 1, + (a, b) => { + return a[1] < b[1] + }, + randomPivotIndexSelect + ) +} + /** * @param array * @param k @@ -166,65 +217,14 @@ function selectRecursion ( } /** - * @param tasksMap - * @returns - */ -function quickSelectLoop (tasksMap) { - const tasksArray = Array.from(tasksMap) - - return selectLoop(tasksArray, 0, 0, tasksArray.length - 1, (a, b) => { - return a[1] < b[1] - }) -} - -/** - * @param tasksMap - * @returns - */ -function quickSelectLoopRandomPivot (tasksMap) { - const tasksArray = Array.from(tasksMap) - - return selectLoop( - tasksArray, - 0, - 0, - tasksArray.length - 1, - (a, b) => { - return a[1] < b[1] - }, - randomPivotIndexSelect - ) -} - -/** - * @param tasksMap - * @returns - */ -function quickSelectRecursion (tasksMap) { - const tasksArray = Array.from(tasksMap) - - return selectRecursion(tasksArray, 0, 0, tasksArray.length - 1, (a, b) => { - return a[1] < b[1] - }) -} - -/** - * @param tasksMap - * @returns + * @param array + * @param index1 + * @param index2 */ -function quickSelectRecursionRandomPivot (tasksMap) { - const tasksArray = Array.from(tasksMap) - - return selectRecursion( - tasksArray, - 0, - 0, - tasksArray.length - 1, - (a, b) => { - return a[1] < b[1] - }, - randomPivotIndexSelect - ) +function swap (array, index1, index2) { + const tmp = array[index1] + array[index1] = array[index2] + array[index2] = tmp } group('Quick select', () => { diff --git a/random.mjs b/random.mjs index b616a7a..8b4b2a6 100644 --- a/random.mjs +++ b/random.mjs @@ -13,16 +13,16 @@ const maximum = 281474976710655 * @param min * @returns */ -function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { +function getRandomInteger (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 !== 0) { min = Math.ceil(min) - return Math.floor(secureRandom() * (max - min + 1)) + min + return Math.floor(Math.random() * (max - min + 1)) + min } - return Math.floor(secureRandom() * (max + 1)) + return Math.floor(Math.random() * (max + 1)) } /** @@ -30,19 +30,16 @@ function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { * @param min * @returns */ -function getSecureRandomIntegerWithRandomValues ( - max = Number.MAX_SAFE_INTEGER, - min = 0 -) { +function getSecureRandomInteger (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 !== 0) { min = Math.ceil(min) - return Math.floor(secureRandomWithRandomValues() * (max - min + 1)) + min + return Math.floor(secureRandom() * (max - min + 1)) + min } - return Math.floor(secureRandomWithRandomValues() * (max + 1)) + return Math.floor(secureRandom() * (max + 1)) } /** @@ -50,16 +47,19 @@ function getSecureRandomIntegerWithRandomValues ( * @param min * @returns */ -function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { +function getSecureRandomIntegerWithRandomValues ( + 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 !== 0) { min = Math.ceil(min) - return Math.floor(Math.random() * (max - min + 1)) + min + return Math.floor(secureRandomWithRandomValues() * (max - min + 1)) + min } - return Math.floor(Math.random() * (max + 1)) + return Math.floor(secureRandomWithRandomValues() * (max + 1)) } group('Random Integer Generator', () => { -- 2.34.1