From: Jérôme Benoit Date: Mon, 24 Oct 2022 13:50:39 +0000 (+0200) Subject: Fix random number generators input checks X-Git-Tag: v1.1.86~20 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b3aa9f53cdbf6dac72396c4392e11dcc4c37df65;p=e-mobility-charging-stations-simulator.git Fix random number generators input checks Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index e8e658d1..d396cfa9 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -106,7 +106,7 @@ export default class Utils { } public static getRandomFloat(max = Number.MAX_VALUE, min = 0, negative = false): number { - if (max < min || min < 0 || max < 0) { + if (max < min || max < 0 || min < 0) { throw new RangeError('Invalid interval'); } const randomPositiveFloat = crypto.randomBytes(4).readUInt32LE() / 0xffffffff; @@ -115,14 +115,11 @@ export default class Utils { } public static getRandomInteger(max = Number.MAX_SAFE_INTEGER, min = 0): number { - if (max < 0) { + if (max < min || max < 0 || min < 0) { throw new RangeError('Invalid interval'); } max = Math.floor(max); if (!Utils.isNullOrUndefined(min) && min !== 0) { - if (max < min || min < 0) { - throw new RangeError('Invalid interval'); - } min = Math.ceil(min); return Math.floor(Utils.secureRandom() * (max - min + 1)) + min; }