1 const crypto
= require('crypto')
4 * Generate a cryptographically secure random number in the [0,1[ range
8 function secureRandom () {
9 return crypto
.randomBytes(4).readUInt32LE() / 0x100000000
17 function generateRandomInteger (max
, min
= 0) {
19 throw new RangeError('Invalid interval')
23 if (max
< min
|| min
< 0) {
24 throw new RangeError('Invalid interval')
27 return Math
.floor(secureRandom() * (max
- min
+ 1)) + min
29 return Math
.floor(secureRandom() * (max
+ 1))
36 async
function sleep (ms
) {
37 return new Promise(resolve
=> setTimeout(resolve
, ms
))
40 const LIST_FORMATTER
= new Intl
.ListFormat('en-US', {
45 module
.exports
= { generateRandomInteger
, sleep
, secureRandom
, LIST_FORMATTER
}