📜  给定范围js的随机数 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:02:54.538000             🧑  作者: Mango

代码示例3
const randomNumber = ({ min, max } = { min: 0, max: 1 }) => {
  if (min >= max) {
    throw Error(
      `minimum value (${min}) is larger than or equal to maximum value (${max})`
    );
  }

  return Math.floor(Math.random() * Math.floor(max - min + 1) + min);
};

// Usage: random number between 10 and 100.
const n = randomNumber({ min: 10, max: 100 });