X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=benchmark-utils.js;h=a9dd58713e5edc4ae244a4098219660032d79e16;hb=843c1afbc664f667f75bc554b61e109e2b03e2b5;hp=121ef4ad375b6af43d43924677b070f88c024843;hpb=99d76a65d58d151b760876e1e8e9178be3624365;p=benchmarks-js.git diff --git a/benchmark-utils.js b/benchmark-utils.js index 121ef4a..a9dd587 100644 --- a/benchmark-utils.js +++ b/benchmark-utils.js @@ -2,7 +2,6 @@ const crypto = require('crypto') /** * Generate a cryptographically secure random number in the [0,1[ range - * * @returns */ function secureRandom () { @@ -11,8 +10,7 @@ function secureRandom () { /** * Generate a cryptographically secure random number in the [0,1[ range - * - * @param + * @returns */ function secureRandomWithRandomValues () { return crypto.getRandomValues(new Uint32Array(1))[0] / 0x100000000 @@ -39,16 +37,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 } /**