合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
984 B

  1. // @ts-nocheck
  2. /**
  3. *
  4. * @param min
  5. * @param max
  6. * @param fixed 0
  7. * @returns
  8. */
  9. export function random(min: number, max: number, fixed: number = 0):number {
  10. // 将 min 和 max 转换为数字类型
  11. // min = +min || 0;
  12. // max = +max || 0;
  13. // 计算随机数范围内的一个随机数
  14. const num = Math.random() * (max - min) + min;
  15. // 如果 fixed 参数为 0,则返回四舍五入的整数随机数;否则保留固定小数位数
  16. // Number
  17. return fixed == 0 ? Math.round(num) : parseFloat(num.toFixed(fixed));
  18. }
  19. // 示例
  20. // console.log(random(0, 10)); // 输出:在 0 和 10 之间的一个整数随机数
  21. // console.log(random(0, 1, 2)); // 输出:在 0 和 1 之间的一个保留两位小数的随机数
  22. // console.log(random(1, 100, 3)); // 输出:在 1 和 100 之间的一个保留三位小数的随机数