-import crypto from 'crypto'
+import { randomInt } from 'node:crypto'
import Benchmark from 'benny'
import {
secureRandom,
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
}
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
}
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
}
),
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)
format: 'table.html',
details: true
})
-).catch(err => {
- console.error(err)
-})
+).catch(console.error)