使用crypto生成随机数

This commit is contained in:
zhl 2019-05-09 13:55:30 +08:00
parent 23cee74ef9
commit 526b96ed78
2 changed files with 8 additions and 1 deletions

View File

@ -16,6 +16,7 @@
"license": "ISC",
"dependencies": {
"async": "^2.6.2",
"biguint-format": "^1.0.1",
"bluebird": "^3.5.3",
"bunyan": "^1.8.12",
"cheerio": "^1.0.0-rc.3",

View File

@ -1,3 +1,5 @@
import crypto from "crypto";
import format from 'biguint-format';
export default {
/**
@ -35,8 +37,12 @@ export default {
}
return result;
},
// randomNum(minNum, maxNum) {
// return parseInt(Math.random()*(maxNum-minNum+1)+minNum, 10);
// },
randomNum(minNum, maxNum) {
return parseInt(Math.random()*(maxNum-minNum+1)+minNum, 10);
let x = format(crypto.randomBytes(4), 'dec');
return parseInt(x / Math.pow(2, 4 * 8) * (maxNum + 1 - minNum) + minNum);
},
getContentByReg(str, reg) {
const contents = str.match(reg);