From: Jérôme Benoit Date: Fri, 10 Feb 2023 16:10:55 +0000 (+0100) Subject: Refactor generateRandomFloat() helper X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=768c0780fd2fbd9c3f382c27f73ca267a5fe1fe6;p=benchmarks-js.git Refactor generateRandomFloat() helper Signed-off-by: Jérôme Benoit --- diff --git a/benchmark-utils.js b/benchmark-utils.js index e39708b..c123243 100644 --- a/benchmark-utils.js +++ b/benchmark-utils.js @@ -39,16 +39,16 @@ function generateRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { * * @param max * @param min - * @param negative * @returns */ -function generateRandomFloat (max = Number.MAX_VALUE, min = 0, negative = true) { - if (max < min || max < 0 || min < 0) { +function generateRandomFloat (max = Number.MAX_VALUE, min = 0) { + if (max < min) { + throw new RangeError('Invalid interval') + } + if (max - min === Infinity) { throw new RangeError('Invalid interval') } - const randomPositiveFloat = crypto.randomBytes(4).readUInt32LE() / 0xffffffff - const sign = negative && randomPositiveFloat < 0.5 ? -1 : 1 - return sign * (randomPositiveFloat * (max - min) + min) + return (crypto.randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min } /**