40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
/**
|
||
* 生成随机数
|
||
* @param min 最小值
|
||
* @param max 最大值
|
||
* @returns 随机数
|
||
*/
|
||
export function randomNum(min: number, max: number) {
|
||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||
}
|
||
/**
|
||
* 生成一个长度在 0 到 n 之间的随机字符串。
|
||
* @param n 最大字符串长度。
|
||
* @returns 随机生成的字符串。
|
||
*/
|
||
export function generateRandomString(n: number): string {
|
||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||
const length = Math.floor(Math.random() * (n + 1));
|
||
let result = '';
|
||
|
||
for (let i = 0; i < length; i++) {
|
||
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
||
}
|
||
|
||
return result;
|
||
}
|
||
/**
|
||
* 将秒转换为“分:秒”的格式
|
||
* @param seconds - 输入的秒数
|
||
* @returns 格式化后的字符串,格式为“MM:SS”
|
||
*/
|
||
export function formatSeconds(seconds: number): string {
|
||
// 计算分钟数
|
||
const minutes = Math.floor(seconds / 60);
|
||
// 计算剩余的秒数
|
||
const remainingSeconds = seconds % 60;
|
||
|
||
// 返回格式化后的字符串,确保分钟和秒数都是两位数
|
||
return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
|
||
}
|