X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=random.js;h=f4fd9f0bd53f9182f13541be0b3bb17bc7c5e27c;hb=35ce7574d092f245337dffbe87e17e1ac85033f0;hp=ed5acd6843685ecd54fe66855b659de6e31a3dfe;hpb=e2bfb54955f0d0138b5c83a918c96112a5710d52;p=benchmarks-js.git diff --git a/random.js b/random.js index ed5acd6..f4fd9f0 100644 --- a/random.js +++ b/random.js @@ -9,8 +9,11 @@ const maximum = Number.MAX_SAFE_INTEGER * @returns */ function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { + if (max < min || max < 0 || min < 0) { + throw new RangeError('Invalid interval') + } max = Math.floor(max) - if (min) { + if (min != null && min !== 0) { min = Math.ceil(min) return Math.floor(secureRandom() * (max - min + 1)) + min } @@ -23,8 +26,11 @@ function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { * @returns */ function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { + if (max < min || max < 0 || min < 0) { + throw new RangeError('Invalid interval') + } max = Math.floor(max) - if (min) { + if (min != null && min !== 0) { min = Math.ceil(min) return Math.floor(Math.random() * (max - min + 1)) + min }