X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Futils%2FUtils.ts;h=ab2a8a5f26302bf23d52aa3bf0a21e407d29ace8;hb=2f45578b9925ac9137d92898216c8c4c0b0bacdc;hp=34ace09c50fe4bcbddf5b21015e9482fa72532c2;hpb=27f08ad31f3e69c0681005edc6e9fec49d0450c2;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 34ace09c..ab2a8a5f 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -121,13 +121,15 @@ export default class Utils { return result; } - public static getRandomFloat(max = Number.MAX_VALUE, min = 0, negative = false): number { - if (max < min || max < 0 || min < 0) { + public static getRandomFloat(max = Number.MAX_VALUE, min = 0): number { + 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 randomPositiveFloat * (max - min) + min; } public static getRandomInteger(max = Constants.MAX_RANDOM_INTEGER, min = 0): number {