mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-12-15 07:05:21 +08:00
文件分类新增压缩解压,数学分类新增统计、几何、三角、单位换算
This commit is contained in:
@@ -1,23 +1,63 @@
|
||||
const randomInt = (start, end) => {
|
||||
return Math.round(Math.random() * (end - start) + start);
|
||||
/**
|
||||
* 生成随机数
|
||||
* @param {number} min 最小值
|
||||
* @param {number} max 最大值
|
||||
* @param {number} count 生成数量
|
||||
* @param {number} decimals 小数位数
|
||||
* @returns {number|number[]} 随机数或随机数数组
|
||||
*/
|
||||
function number(min = 0, max = 100, count = 1, decimals = 0) {
|
||||
if (min >= max) {
|
||||
throw new Error("最小值必须小于最大值");
|
||||
}
|
||||
if (count < 1) {
|
||||
throw new Error("生成数量必须大于0");
|
||||
}
|
||||
if (decimals < 0) {
|
||||
throw new Error("小数位数不能为负数");
|
||||
}
|
||||
|
||||
const factor = Math.pow(10, decimals);
|
||||
const generate = () => {
|
||||
const random = Math.random() * (max - min) + min;
|
||||
return Math.round(random * factor) / factor;
|
||||
};
|
||||
|
||||
if (count === 1) {
|
||||
return generate();
|
||||
}
|
||||
|
||||
return Array.from({ length: count }, generate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机整数
|
||||
* @param {number} min 最小值
|
||||
* @param {number} max 最大值
|
||||
* @param {number} count 生成数量
|
||||
* @returns {number|number[]} 随机整数或随机整数数组
|
||||
*/
|
||||
function integer(min = 0, max = 100, count = 1) {
|
||||
min = Math.ceil(min);
|
||||
max = Math.floor(max);
|
||||
|
||||
if (min >= max) {
|
||||
throw new Error("最小值必须小于最大值");
|
||||
}
|
||||
if (count < 1) {
|
||||
throw new Error("生成数量必须大于0");
|
||||
}
|
||||
|
||||
const generate = () => Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
|
||||
if (count === 1) {
|
||||
return generate();
|
||||
}
|
||||
|
||||
return Array.from({ length: count }, generate);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
number,
|
||||
integer,
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user