From: Jérôme Benoit Date: Mon, 24 Oct 2022 12:00:25 +0000 (+0200) Subject: Fix random integer generator X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=da504870febba0179ba92a342528d6f4384e9534;p=benchmarks-js.git Fix random integer generator Signed-off-by: Jérôme Benoit --- diff --git a/benchmark-utils.js b/benchmark-utils.js index 164cfc6..50aa39c 100644 --- a/benchmark-utils.js +++ b/benchmark-utils.js @@ -19,7 +19,7 @@ function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { throw new RangeError('Invalid interval') } max = Math.floor(max) - if (min) { + if (min != null && min !== 0) { if (max < min || min < 0) { throw new RangeError('Invalid interval') } diff --git a/quick-select.js b/quick-select.js index 4be2205..d45a9e8 100644 --- a/quick-select.js +++ b/quick-select.js @@ -63,7 +63,7 @@ const defaultPivotIndexSelect = (leftIndex, rightIndex) => { } const randomPivotIndexSelect = (leftIndex, rightIndex) => { - return generateRandomInteger(leftIndex, rightIndex) + return generateRandomInteger(rightIndex, leftIndex) } /** diff --git a/random.js b/random.js index ed5acd6..ee5cc61 100644 --- a/random.js +++ b/random.js @@ -10,7 +10,7 @@ const maximum = Number.MAX_SAFE_INTEGER */ function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { max = Math.floor(max) - if (min) { + if (min != null && min !== 0) { min = Math.ceil(min) return Math.floor(secureRandom() * (max - min + 1)) + min } @@ -24,7 +24,7 @@ function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { */ function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { max = Math.floor(max) - if (min) { + if (min != null && min !== 0) { min = Math.ceil(min) return Math.floor(Math.random() * (max - min + 1)) + min }