mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
19 lines
492 B
19 lines
492 B
4 years ago
|
'use strict';
|
||
|
|
||
|
const { promisify } = require('util')
|
||
|
, randomBytes = promisify(require('crypto').randomBytes);
|
||
|
|
||
4 years ago
|
//NOTE: should only be used for ints, floors your inputs
|
||
4 years ago
|
module.exports = async (min, max) => {
|
||
4 years ago
|
min = Math.floor(min);
|
||
|
max = Math.floor(max);
|
||
4 years ago
|
if (max <= min) return min;
|
||
4 years ago
|
const mod = max - min + 1;
|
||
4 years ago
|
const div = (((0xffffffff - (mod-1)) / mod) | 0) + 1;
|
||
|
let g
|
||
4 years ago
|
do {
|
||
4 years ago
|
g = (await randomBytes(4)).readUInt32LE();
|
||
|
} while (g > div * mod - 1);
|
||
|
return ((g / div) | 0) + min;
|
||
|
};
|