From 526b96ed78bdb91c3e1c3287995693435b8711f5 Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 9 May 2019 13:55:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8crypto=E7=94=9F=E6=88=90?= =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/utils/string.util.js | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 1f87311..ae3c419 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils/string.util.js b/src/utils/string.util.js index 13f8d3f..edd4219 100644 --- a/src/utils/string.util.js +++ b/src/utils/string.util.js @@ -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);