新增数学计算和随机数

This commit is contained in:
fofolee
2025-01-06 00:28:54 +08:00
parent 38617df03c
commit cb85aaa790
4 changed files with 149 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ const string = require("./string");
const crypto = require("./crypto");
const buffer = require("./buffer");
const zlib = require("./zlib");
const random = require("./random");
module.exports = {
...encoder,
@@ -12,4 +13,5 @@ module.exports = {
...crypto,
buffer,
zlib,
random,
};

View File

@@ -0,0 +1,23 @@
const randomInt = (start, end) => {
return Math.round(Math.random() * (end - start) + start);
};
const random = (isInt = false, start, end) => {
if (!start && !end) {
return isInt ? randomInt(0, 1000000) : Math.random();
}
if (!end) {
end = Math.abs(randomInt(0, 1000000) - start);
}
if (!start) {
start = 0;
}
// 有start和end返回区间随机数
const random = Math.random() * (end - start) + start;
return isInt ? Math.round(random) : random;
};
module.exports = random;