From 804a63757c95b0f5b802233ba678cac3d1a7a886 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 22 Jan 2023 20:03:45 +0100 Subject: [PATCH] Benchmark inclusive interval crypto.randomInt based integer generator MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- random.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/random.js b/random.js index 2bdd430..272d8e6 100644 --- a/random.js +++ b/random.js @@ -5,7 +5,7 @@ const { } = require('./benchmark-utils') const crypto = require('crypto') -const maximum = 281474976710655 +const maximum = 281474976710654 /** * @param max @@ -72,8 +72,13 @@ Benchmark.suite( getSecureRandomIntegerWithRandomValues(maximum) } ), - Benchmark.add('Crypto random integer generator', () => { - crypto.randomInt(maximum) + Benchmark.add('Crypto random integer generator', (max = maximum, min = 0) => { + max = Math.floor(max) + if (min !== undefined && min !== 0) { + min = Math.ceil(min) + return Math.floor(crypto.randomInt(min, max + 1)) + } + return Math.floor(crypto.randomInt(max + 1)) }), Benchmark.add('Math random integer generator', () => { getRandomInteger(maximum) -- 2.34.1