X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=random.mjs;h=f01ab2ba009d3c20c03627f259d9d6849cf7f837;hb=d6c10c05afa97862a2b35a1a7d000bc8462d1708;hp=f814d3d37344900edacfe5d5190f106d64f92c19;hpb=f522d7b906f8a45a7e4fd349abe3f83bf8fc3d69;p=benchmarks-js.git diff --git a/random.mjs b/random.mjs index f814d3d..f01ab2b 100644 --- a/random.mjs +++ b/random.mjs @@ -1,9 +1,9 @@ -import crypto from 'crypto' +import { randomInt } from 'node:crypto' import Benchmark from 'benny' import { secureRandom, secureRandomWithRandomValues -} from './benchmark-utils.js' +} from './benchmark-utils.mjs' const maximum = 281474976710654 @@ -17,7 +17,7 @@ function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { throw new RangeError('Invalid interval') } max = Math.floor(max) - if (min != null && min !== 0) { + if (min !== 0) { min = Math.ceil(min) return Math.floor(secureRandom() * (max - min + 1)) + min } @@ -37,7 +37,7 @@ function getSecureRandomIntegerWithRandomValues ( throw new RangeError('Invalid interval') } max = Math.floor(max) - if (min != null && min !== 0) { + if (min !== 0) { min = Math.ceil(min) return Math.floor(secureRandomWithRandomValues() * (max - min + 1)) + min } @@ -54,7 +54,7 @@ function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) { throw new RangeError('Invalid interval') } max = Math.floor(max) - if (min != null && min !== 0) { + if (min !== 0) { min = Math.ceil(min) return Math.floor(Math.random() * (max - min + 1)) + min } @@ -74,11 +74,11 @@ Benchmark.suite( ), Benchmark.add('Crypto random integer generator', (max = maximum, min = 0) => { max = Math.floor(max) - if (min != null && min !== 0) { + if (min !== 0) { min = Math.ceil(min) - return Math.floor(crypto.randomInt(min, max + 1)) + return Math.floor(randomInt(min, max + 1)) } - return Math.floor(crypto.randomInt(max + 1)) + return Math.floor(randomInt(max + 1)) }), Benchmark.add('Math random integer generator', () => { getRandomInteger(maximum) @@ -100,6 +100,4 @@ Benchmark.suite( format: 'table.html', details: true }) -).catch(err => { - console.error(err) -}) +).catch(console.error)