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
16 function generateRandomInteger (max
, min
= 0) {
18 throw new RangeError('Invalid interval')
22 if (max
< min
|| min
< 0) {
23 throw new RangeError('Invalid interval')
26 return Math
.floor(secureRandom() * (max
- min
+ 1)) + min
28 return Math
.floor(secureRandom() * (max
+ 1))
34 async
function sleep (ms
) {
35 return new Promise(resolve
=> setTimeout(resolve
, ms
))
38 const LIST_FORMATTER
= new Intl
.ListFormat('en-US', {
43 module
.exports
= { generateRandomInteger
, sleep
, secureRandom
, LIST_FORMATTER
}