From da504870febba0179ba92a342528d6f4384e9534 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 24 Oct 2022 14:00:25 +0200 Subject: [PATCH] Fix random integer generator MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmark-utils.js | 2 +- quick-select.js | 2 +- random.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) 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 } -- 2.34.1