add function randomNumber
This commit is contained in:
parent
39173e47bb
commit
2224e9c00f
16
index.js
16
index.js
@ -117,6 +117,22 @@ module.exports = {
|
||||
return parseInt(port)
|
||||
},
|
||||
|
||||
randomNumber ({ length, min, max } = {}) {
|
||||
// 长度为 length 的随机数字,或者 (min||0) <= num < max
|
||||
var num = 0
|
||||
if (typeof length === 'number' && length > 0) {
|
||||
num = parseInt(Math.random() * Math.pow(10, length))
|
||||
num = num.toString().padStart(length, '0')
|
||||
} else if (typeof max === 'number' && max > 0) {
|
||||
min = typeof min === 'number' && min >= 0 ? min : 0
|
||||
num = parseInt(Math.random() * (max - min)) + min
|
||||
} else {
|
||||
// 如果 option 为空
|
||||
num = Math.random()
|
||||
}
|
||||
return num
|
||||
},
|
||||
|
||||
/**
|
||||
* 用户编号转邀请码
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user